Notification Listener Service Not Started When Running In Debug Mode
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?
- I made some initialization in
NLService
class'onConnect()
method. I moved all this initialization toonListenerConnected()
, adding ahandler.postDelayed(runnable, 500);
. - I created an object of a class (Let's call it
MyHandlerClass
) within theNLService
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 theNotificationListenerService
: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 NLService
s' 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"