2

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:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
        startActivity(intent)
    }

    override fun onResume() {
        super.onResume()
        val isServiceRunning = isMyServiceRunning(NLService::class.java)
        Log.i("MainActivity", "service running: " + isServiceRunning)

    }

    private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
        val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (service in manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.name == service.service.className) {
                return true
            }
        }
        return false
    }
}

The Service:

class NLService : NotificationListenerService() {

    private val TAG: String? = "NLService"

    override fun onBind(intent: Intent): IBinder? {
        Log.i(TAG, "onBind()")
        return super.onBind(intent)
    }

    override fun onCreate() {
        super.onCreate()
        Log.i(TAG, "onCreate()")
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        Log.i(TAG, "onNotificationPosted() sbn: $sbn")
        super.onNotificationPosted(sbn)
    }
}

Of course I added this in manifest:

<service
    android:name=".NLService"
    android:label="MyNLService"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>

</service>

When starting the app in debug mode then I always get the log output service running: false in onResume. The value is true when starting normally, without debug. What is going wrong and how to fix that?

unlimited101
  • 3,653
  • 4
  • 22
  • 41
  • This is likely a timing problem. Android starts your `NotificationListenerService` and you may not be giving it enough time. – David Wasser May 14 '18 at 16:21
  • What do you mean by not giving it enough time? Could you describe a bit more in detail? Do you think I maybe do to fast startup in onListenerConnected()? So should I initialize things in that method with a postDelayed? Or do you think I shall call requestRebind() before onListenerConnected() every time? Calling requestRebind() for now is my fallback for re-connecting the service as soon as I detected that the service is not running. – unlimited101 May 15 '18 at 02:39
  • You should post more code or try to determine why the `Service` is crashing. You've not given enough information to help you out. – David Wasser May 15 '18 at 17:42

1 Answers1

0

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.:

fun getActiveNotifications(): Array<StatusBarNotification>?
{
    return if (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.

unlimited101
  • 3,653
  • 4
  • 22
  • 41
  • But why is the server crashing? Post your logcat with exception and stacktrace. You may need to remove logcat filters to see the problem. – David Wasser May 15 '18 at 17:39