我的應用程序出現問題,我想報告此錯誤。應用程序崩潰後Notification Listener Service不起作用
我開發了可以使用NotificationListenerService來檢索通知的應用程序。
它運作良好。
但NotificationListenerService類有我想的問題。
因爲,如果應用程序崩潰,應用程序無法抓取通知, 直到手機重新啓動。
是誰能解決這個問題?
請幫幫我。
錯誤非常明顯!但它不容易找到解決方案....
我的應用程序出現問題,我想報告此錯誤。應用程序崩潰後Notification Listener Service不起作用
我開發了可以使用NotificationListenerService來檢索通知的應用程序。
它運作良好。
但NotificationListenerService類有我想的問題。
因爲,如果應用程序崩潰,應用程序無法抓取通知, 直到手機重新啓動。
是誰能解決這個問題?
請幫幫我。
錯誤非常明顯!但它不容易找到解決方案....
我看到完全一樣的。我發現的唯一「解決方案」是讓通知偵聽器在單獨的進程中運行。然後,如果應用程序的其餘部分崩潰,則不會停止偵聽器。所以它只是特別通知偵聽器服務崩潰,需要重啓。
雖然似乎是一個可怕的和過度複雜的解決方案。
如果你已經權限,則:
在服務類或其他服務/活動,你可以切換「組件hability」聽通知:
public void tryReconnectService() {
toggleNotificationListenerService();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ComponentName componentName =
new ComponentName(getApplicationContext(), NotificationReaderV2Service.class);
//It say to Notification Manager RE-BIND your service to listen notifications again inmediatelly!
requestRebind(componentName);
}
}
/**
* Try deactivate/activate your component service
*/
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, NotificationReaderV2Service.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this, NotificationReaderV2Service.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
您的通知監聽器,是一個服務,它可以通過系統殺死,你可以做你的服務作爲前途,以大大降低系統將殺死你的服務的可能性。
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.d(TAG, "Service Reader Connected");
Notification not = createNotification();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.notify(NOTIFICATION_ID, not);
}
startForeground(NOTIFICATION_ID, not);
//Alarm to auto - send Intents to Service to reconnect, you can ommit next line.
alarmIt();
}
如果你喜歡這樣更「安全」的,你可以編程不是友好的電池警報時,儘量使用不準確的警報,請,用戶的電池會很高興:
private void alarmIt() {
Log.d(TAG, "ALARM PROGRAMMATED at"+HotUtils.formatDate(new Date()));
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + 1);
Intent intent = new Intent(this, NotificationReaderV2Service.class);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.setAction(REBIND_ACTION);
PendingIntent pendingIntent = PendingIntent.getService(this, 0,
intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//The alarms that are repeated are inaccurate by default, use RTC_WAKE_UP at your convenience.
//Alarm will fire every minute, CHANGE THIS iF DO YOU CAN, you can't use less than 1 minute to repeating alarms.
manager.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
}
旁邊看了意圖重新連接服務綁定:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Notification service onStartCommandCalled");
if (intent!=null && !HotUtils.isNullOrEmpty(intent.getAction()) && intent.getAction().equals(REBIND_ACTION)){
Log.d(TAG, "TRYING REBIND SERVICE at "+HotUtils.formatDate(new Date()));
tryReconnectService();//switch on/off component and rebind
}
//START_STICKY to order the system to restart your service as soon as possible when it was killed.
return START_STICKY;
}
請記住,在做所有這些步驟你可以確定你的服務無論如何都會被系統殺死,但是這個代碼將會重啓服務並且使其更難以殺死它。
也許,你應該考慮使用PARTIAL_WAKE_LOCK與您的服務,並在過程中獨立地執行它(:遙控器),如果你想更確定性(這也許是沒用)
我想添加經常出現的常見錯誤,請勿重寫onBind和onUnbind方法或覆蓋INTENT ACTION。 這會導致您的服務不能連接,並且永遠不會運行.ListenerConnected 保持原意,在大多數情況下,您無需對其進行編輯。
完成本答案issuetracker.google.com。這仍然工作正常嗎? –
我的服務正在運行37天,倖存的Playstore更新和類似事件。 –
它也適用於我。這是一個很好的回答:) –