运行 处于调试模式时,通知侦听器服务未启动
Notification Listener Service not started when running in debug mode
我正在使用 NotificationListenerService 构建应用程序。但是总是当我 运行 处于调试模式的应用程序时,服务不会启动。我将代码缩减为以下内容:
我的活动:
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)
}
}
当然我在清单中添加了这个:
<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
。正常启动时该值为真,无需调试。出了什么问题以及如何解决?
好吧,最后我没有一个完整的解决方案,而是一种接近工作解决方案的改进。
那么我的原始应用程序代码实际上存在哪些技术问题?我是怎么解决的?
- 我在
NLService
class' onConnect()
方法中做了一些初始化。我将所有这些初始化移动到 onListenerConnected()
,添加了 handler.postDelayed(runnable, 500);
.
- 我在
NLService
class 中创建了一个 class(我们称它为 MyHandlerClass
)的对象,并将对服务的引用传递给它。这不是一个好的解决方案,因为 Android documentation says something about many methods 在 NotificationListenerService
中:
The service should wait for the onListenerConnected() event before performing this operation.
所以在 MyHandlerClass
我打了一个 nlService.getActiveNotifications()
。此调用是在 可能 之前 Android 调用 NLService
s' onListenerConnected
。所以我为 NLService
中的方法制作了包装器,例如:
fun getActiveNotifications(): Array<StatusBarNotification>?
{
return if (isConnected)
{
super.getActiveNotifications()
}
else
{
null
}
}
然后我在 onListenerConnected()
和 onListenerDisconnected()
中切换了我的布尔变量 isConnected
现在,当 运行 应用程序处于调试模式时,服务仍然会崩溃。但是 运行 在正常模式下,崩溃的数量可以通过所描述的改进来减少。
我正在使用 NotificationListenerService 构建应用程序。但是总是当我 运行 处于调试模式的应用程序时,服务不会启动。我将代码缩减为以下内容:
我的活动:
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)
}
}
当然我在清单中添加了这个:
<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
。正常启动时该值为真,无需调试。出了什么问题以及如何解决?
好吧,最后我没有一个完整的解决方案,而是一种接近工作解决方案的改进。
那么我的原始应用程序代码实际上存在哪些技术问题?我是怎么解决的?
- 我在
NLService
class'onConnect()
方法中做了一些初始化。我将所有这些初始化移动到onListenerConnected()
,添加了handler.postDelayed(runnable, 500);
. - 我在
NLService
class 中创建了一个 class(我们称它为MyHandlerClass
)的对象,并将对服务的引用传递给它。这不是一个好的解决方案,因为 Android documentation says something about many methods 在NotificationListenerService
中:The service should wait for the onListenerConnected() event before performing this operation.
所以在 MyHandlerClass
我打了一个 nlService.getActiveNotifications()
。此调用是在 可能 之前 Android 调用 NLService
s' onListenerConnected
。所以我为 NLService
中的方法制作了包装器,例如:
fun getActiveNotifications(): Array<StatusBarNotification>?
{
return if (isConnected)
{
super.getActiveNotifications()
}
else
{
null
}
}
然后我在 onListenerConnected()
和 onListenerDisconnected()
isConnected
现在,当 运行 应用程序处于调试模式时,服务仍然会崩溃。但是 运行 在正常模式下,崩溃的数量可以通过所描述的改进来减少。