我正在编写一个应用程序,当电池电量达到一定水平时,wifi 将自动关闭完成,我的应用程序在屏幕打开时工作正常,但当屏幕锁定时它什么也不做。
我的应用程序就像,启动活动,用户单击按钮启动服务,服务将注册广播接收器以获取当前电池电量,广播接收器将查看电池电量是否达到限制并决定是否关闭完成的wifi
我尝试使用 isScreenOn() 来查找屏幕是打开还是关闭,但这也不起作用
这是我的代码
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<=90){
if(isScreenOn==true){
if(isCharging == true && wifimanager.isWifiEnabled()){
//not do anything
}else if(isCharging == false && wifimanager.isWifiEnabled()){
wifimanager.setWifiEnabled(false);
}
}else{
if(isCharging == true && wifimanager.isWifiEnabled()){
//not do anything
}else if(isCharging == false && 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;
}
}