Skip to content Skip to sidebar Skip to footer

Notification Listener Service Not Started When Running In Debug Mode

I am building an app using the NotificationListenerService. But always when I run the app in debug mode the Service is not started. I reduced my code to the following: My Acticity:

Solution 1:

Okay, finally I don't have a complete solution but a kind of improvement which is close to a working solution.

So what are actually the technical problems in my original app code? And how did I solve them?

  1. I made some initialization in NLService class' onConnect() method. I moved all this initialization to onListenerConnected(), adding a handler.postDelayed(runnable, 500);.
  2. I created an object of a class (Let's call it MyHandlerClass) within the NLService class and passed a reference to the Service into it. This is not a good solution because Android documentation says something about many methods within the NotificationListenerService:

    The service should wait for the onListenerConnected() event before performing this operation.

So in MyHandlerClass I called a nlService.getActiveNotifications(). This call was made maybe before Android called NLServices' onListenerConnected. So I made wrappers for methods inside NLService, like e.g.:

fungetActiveNotifications(): Array<StatusBarNotification>?
{
    returnif (isConnected)
    {
        super.getActiveNotifications()
    }
    else
    {
        null
    }
}

And I toggled my boolean variable isConnected within onListenerConnected()and onListenerDisconnected()

Now the service still crashes when running app in debug mode. But running in normal mode the amount of crashes could be reduced by the described improvements.

Post a Comment for "Notification Listener Service Not Started When Running In Debug Mode"