Android 如何使用NotificationListener服务修复电池过度耗电

Android 如何使用NotificationListener服务修复电池过度耗电,android,battery,notification-listener,Android,Battery,Notification Listener,我需要检查我的应用程序的当前未读通知,我正在使用通知侦听器服务来完成此操作。然而,这个解决方案似乎在不断地检查导致巨大电池耗电问题的后台通知,准确地说是夜间23%的后台耗电。是否有一种方法可以让我仅在手机处于特定状态(特别是锁定屏幕)时获取活动通知 公共类NotificationListener扩展了NotificationListenerService{ 类CancelNotificationReceiver扩展了BroadcastReceiver{ @凌驾 公共void onReceive(

我需要检查我的应用程序的当前未读通知,我正在使用通知侦听器服务来完成此操作。然而,这个解决方案似乎在不断地检查导致巨大电池耗电问题的后台通知,准确地说是夜间23%的后台耗电。是否有一种方法可以让我仅在手机处于特定状态(特别是锁定屏幕)时获取活动通知

公共类NotificationListener扩展了NotificationListenerService{
类CancelNotificationReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
弦作用;
if(intent!=null&&intent.getAction()!=null){
action=intent.getAction();
if(action.equals(action\u NLS\u CONTROL)){
String命令=intent.getStringExtra(“命令”);
if(TextUtils.equals(命令“cancel_last”)){
如果(mCurrentNotifications!=null&&mCurrentNotificationsCounts>=1){
StatusBarNotification sbnn=getCurrentNotifications()[mCurrentNotificationsCounts-1];
取消通知(sbnn.getPackageName(),sbnn.getTag(),sbnn.getId());
}
}else if(TextUtils.equals(命令“全部取消”)){
取消所有通知();
}
}
}
}
}
@凌驾
public void onCreate(){
super.onCreate();
logNLS(“onCreate…”);
IntentFilter=newintentfilter();
filter.addAction(ACTION\u NLS\u控件);
registerReceiver(mrReceiver,过滤器);
sendMessage(AmmonitorHandler.ActainMessage(事件更新当前编号));
}
@凌驾
公共空间{
super.ondestory();
未注册接收人(mReceiver);
}
@凌驾
公共IBinder onBind(意向){
//a.等于(“b”);
logNLS(“onBind…”);
返回super.onBind(intent);
}
@凌驾
公告发布时的公共作废(状态公告sbn){
updateCurrentNotifications();
logNLS(“onNotificationPosted…”);
logNLS(“具有”+mCurrentNotificationsCounts+“活动通知”);
mPostedNotification=sbn;
}
@凌驾
已删除通知上的公共无效(状态通知sbn){
updateCurrentNotifications();
logNLS(“删除…”);
logNLS(“具有”+mCurrentNotificationsCounts+“活动通知”);
mRemovedNotification=sbn;
}
私有void updateCurrentNotifications(){
试一试{
StatusBarNotification[]activeNos=getActiveNotifications();
if(mCurrentNotifications.size()=0){
mCurrentNotifications.add(空);
}
mCurrentNotifications.set(0,activeNos);
mCurrentNotificationsCounts=活动长度;
}捕获(例外e){
logNLS(“不应该在这里!!”;
e、 printStackTrace();
}
}
公共静态状态通知[]getCurrentNotifications(){
if(mCurrentNotifications.size()=0){
logNLS(“mCurrentNotifications size为零!!”;
返回null;
}
返回mCurrentNotifications.get(0);
}
}