あなたはすでに、その後の権限持っているならば:あなたは通知を聞くために、「コンポーネントの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);
}
あなたの通知リスナーをはSERVICEであり、システムによって殺される可能性があります。サービスをFOREGROUNDとして実行すると、システムがサービスを停止する確率を大幅に低下させることができます。
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 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();
}
、あなたがいない優しいバッテリアラームをプログラミングすることができますので、より多くの「安全」のように、あなたを行うと、ユーザーのバッテリーが幸せになりますしてください不正確なアラームを使用するようにしてください
@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を上書きしたりしないでください。 これは、サービスが接続されず、onListenerConnectedを実行しないようにします。 意図をそのまま維持します。ほとんどの場合、編集する必要はありません。
この回答はissuetracker.google.comで解決しました。これはまだうまくいっていますか? –
私のサービスは37日間稼働し、Playstoreのアップデートやそれに類するイベントで生き残っています。 –
それは私のためにも生き残っています。これは素晴らしい答えです:) –