在调试模式下运行时

在调试模式下运行时

本文介绍了在调试模式下运行时,Notification Listener Service未启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NotificationListenerService构建应用程序.但总是在以调试模式运行该应用程序时,该服务不会启动.我将代码简化为以下内容:

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:

我的行为:

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
    }
}

服务:

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>

在调试模式下启动应用程序时,我总是在onResume中获得日志输出service running: false.正常启动且未调试时,该值为true.发生了什么问题以及如何解决?

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?

推荐答案

好吧,最后我没有一个完整的解决方案,但有一种接近于可行解决方案的改进.

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. 我在NLService类的onConnect()方法中进行了一些初始化.我将所有初始化都移到了onListenerConnected(),并添加了handler.postDelayed(runnable, 500);.
  2. 我在NLService类中创建了一个类的对象(我们称其为MyHandlerClass),并向其中传递了对Service的引用.这不是一个好的解决方案,因为Android 文档对NotificationListenerService中的许多方法进行了说明:
  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:

因此在MyHandlerClass中,我称nlService.getActiveNotifications().在Android呼叫NLService s'onListenerConnected之前,此呼叫是也许进行的.因此,我为NLService中的方法制作了包装器,例如:

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
    }
}

然后我在onListenerConnected()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.

这篇关于在调试模式下运行时,Notification Listener Service未启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:03