2017-07-16 5 views
3

私のアプリに問題があり、このバグを報告したいと思います。アプリがクラッシュした後、通知リスナーサービスが機能しない

私はNotificationListenerServiceを使用して通知をクロールできるアプリを開発しました。

正常に動作します。

しかし、NotificationListenerServiceクラスには、私が思うような問題があります。

アプリがクラッシュした場合、携帯電話がリブートするまで、アプリは通知を一切クロールできません。

この問題を解決できる人は誰ですか?

私を助けてください。

バグは非常にクリアです!しかし、解決策を見つけることは容易ではありません....

答えて

0

私はこれについて全く同じことを見ています。私が見つけた唯一の "解決策"は、通知リスナを別のプロセスで実行させることでした。その後、アプリケーションの残りの部分がクラッシュしても、リスナーは停止しません。したがって、再起動が必要な通知リスナサービスのクラッシュだけが発生します。

しかし、ひどく複雑な解決策と思われます。

4

あなたはすでに、その後の権限持っているならば:あなたは通知を聞くために、「コンポーネントの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を実行しないようにします。 意図をそのまま維持します。ほとんどの場合、編集する必要はありません。

+0

この回答はissuetracker.google.comで解決しました。これはまだうまくいっていますか? –

+0

私のサービスは37日間稼働し、Playstoreのアップデートやそれに類するイベントで生き残っています。 –

+0

それは私のためにも生き残っています。これは素晴らしい答えです:) –

関連する問題