Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid perform function while lock screen
    text
    copied!<p>I am writing an app that when battery reach some level the wifi will automatically shut done, my app works fine while screen is on, but it not do anything when the screnn is lock. </p> <p>My app is like, start activity, user click the button to start the Service, the service will register the broadcastreceiveer to get the current level of battery, and braodcastreceiver will see if battery level reach the limit and decide to shut done wifi or not</p> <p>I have try use isScreenOn() to find whether screen is on or off, but this also not work</p> <p>here is my code</p> <pre><code>public class BatteryMonitor extends Service { /*declear variables*/ private BroadcastReceiver batteryReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent){ /* get battery level*/ //Check is screen is on or not boolean isScreenOn = powermanager.isScreenOn(); //set as 90 just for test function// if(batteryLevel&lt;=90){ if(isScreenOn==true){ if(isCharging == true &amp;&amp; wifimanager.isWifiEnabled()){ //not do anything }else if(isCharging == false &amp;&amp; wifimanager.isWifiEnabled()){ wifimanager.setWifiEnabled(false); } }else{ if(isCharging == true &amp;&amp; wifimanager.isWifiEnabled()){ //not do anything }else if(isCharging == false &amp;&amp; wifimanager.isWifiEnabled()){ wifimanager.setWifiEnabled(false); } } } } }; public void onStart(Intent intent, int startId) { wifimanager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE); powermanager = (PowerManager) getSystemService(Context.POWER_SERVICE); registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); }; @Override public IBinder onBind(Intent i) { // TODO Auto-generated method stub return null; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload