Notification Listener Service not started when run

2019-07-11 14:12发布

问题:

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?

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

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.