NotificationListenerService与API18 RemoteController.OnClientUpdateListener崩溃(4.3) [英] NotificationListenerService with RemoteController.OnClientUpdateListener crashes on API18 (4.3)

查看:719
本文介绍了NotificationListenerService与API18 RemoteController.OnClientUpdateListener崩溃(4.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我要实现新的 RemoteController 的API,它一直引入了API19(奇巧,4.4)。该API需要我来实现从<一个扩展的类href=\"https://developer.android.com/reference/android/service/notification/NotificationListenerService.html\"相对=nofollow> NotificationListenerService (与API18介绍),并实现<一个href=\"https://developer.android.com/reference/android/media/RemoteController.OnClientUpdateListener.html\"相对=nofollow> RemoteController.OnClientUpdateListener 接口。 (关于如何实现这一切的进一步信息可以在这里和发现非常有帮助的例子项目可以发现这里

一切的工作,因为它应该在我的API19(奇巧,4.4)的设备和所有其他API&LT; 18设备。然而问题是,应用程序崩溃马上在API18(果冻豆,4.3),因为android.service.notification.NotificationListenerService正在发送,并开始我的RemoteService,然后用下面的堆栈跟踪瞬间崩溃的应用程序:

 九月五日至24日:32:48.945 893-893 / com。示例E / AndroidRuntime:致命异常:主要
了java.lang.RuntimeException:无法实例化服务com.example.RemoteService:抛出java.lang.ClassNotFoundException:路径上没有找到类com.example.RemoteService:DexPathList [zip文件/数据/应用/ COM。例如,1.apk],nativeLibraryDirectories = [/数据/应用-LIB / com。示例-1 /系统/ lib目录]
        在android.app.ActivityThread.handleCreateService(ActivityThread.java:2561)
        在android.app.ActivityThread.access $ 1600(ActivityThread.java:141)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1338)
        在android.os.Handler.dispatchMessage(Handler.java:99)
        在android.os.Looper.loop(Looper.java:137)
        在android.app.ActivityThread.main(ActivityThread.java:5103)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:525)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        在dalvik.system.NativeStart.main(本机方法)
 抛出java.lang.ClassNotFoundException::未找到路径级com.example.RemoteService:DexPathList [zip文件/data/app/com.example-1.apk\"],nativeLibraryDirectories=[/data所致/app-lib/com.example-1,/系统/ lib目录]
        在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
        在java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        在java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        在android.app.ActivityThread.handleCreateService(ActivityThread.java:2558)
        在android.app.ActivityThread.access $ 1600(ActivityThread.java:141
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1338)
...

这是一个简单的事实造成的,即 RemoteController ,因此实现的接口<一个href=\"https://developer.android.com/reference/android/media/RemoteController.OnClientUpdateListener.html\"相对=nofollow> RemoteController.OnClientUpdateListener 是不可用API18,而是<一个href=\"https://developer.android.com/reference/android/service/notification/NotificationListenerService.html\"相对=nofollow> NotificationListenerService 是。意图不被每个设备上发送的

我到目前为止已经试过


  • 穿上它的抽象性,并构建一个简单的如果(API> = 19)检查。
    我试图通过这样做第二个空的<一个href=\"https://developer.android.com/reference/android/service/notification/NotificationListenerService.html\"相对=nofollow> NotificationListenerService 这将只是工作作为一种电话接通率的服务。我把那第二个服务在我AndroidManifest,所以它接收的意图,而不是,然后我只是把这个如果(API> = 19)复选框在其的onCreate,并从那里开始我的实际RemoteService。其结果是,我的应用程序不会崩溃任何更多的(耶)。不幸的是它也不会在API19奇巧工作了:(。我想这是因为我有直接注册我RemoteService在我AndroidManifest监听器,否则就收不到任何东西。

code片段

RemoteService.java:

 公共类RemoteService扩展NotificationListenerService
    实现RemoteController.OnClientUpdateListener {
...
}

AndroidManifest.xml中:

 &LT;清单...&GT;
    &lt;应用...&GT;
        &LT;服务机器人:名字=com.example.RemoteService
             机器人:标签=@字符串/ APP_NAME
             机器人:权限=android.permission.BIND_NOTIFICATION_LISTENER_SERVICE&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.service.notification.NotificationListenerService/&GT;
            &所述; /意图滤光器&gt;
        &LT; /服务&GT;
    &LT; /用途&gt;
&LT; /清单&GT;


解决方案

解决方案

将一个布尔在价值观和价值-V19的XML文件。它设置为false值,并在真正的价值观V19。现在设置NotificationListenerService的机器人:启用标记这个布尔

./值/ bool_switches.xml:

 &LT;资源&GT;
    &LT;布尔NAME =remotecontrollerservice_enabled&GT;假LT; /布尔&GT;
&LT; /资源&GT;

./值-V19 / bool_switches.xml:

 &LT;资源&GT;
    &LT;布尔NAME =remotecontrollerservice_enabled&GT;真&LT; /布尔&GT;
&LT; /资源&GT;

./ AndroidManifest.xml中:

  ...
&LT;服务机器人:名字=。services.RemoteControllerService
         机器人:标签=@字符串/ scrobblingservice_string
         机器人:权限=android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
         机器人:启用=@布尔/ remotecontrollerservice_enabled&GT;
    &所述;意图滤光器&gt;
        &lt;作用机器人:名字=android.service.notification.NotificationListenerService/&GT;
    &所述; /意图滤光器&gt;
&LT; /服务&GT;
...

The Problem

I want to implement the new RemoteController API, which has been introduced with API19 (Kitkat,4.4). The API requires me to implement a class which extends from NotificationListenerService (introduced with API18) and implements the RemoteController.OnClientUpdateListener interface. (Further information on how to implement all this can be found here and a very helpful example project can be found here)

Everything's working as it should on my API19 (Kitkat,4.4) device and every other API<18 device. However the problem is, that the app crashes right away on API18 (Jelly bean, 4.3), because the "android.service.notification.NotificationListenerService" is being sent and starts my RemoteService, which then crashes the app instantly with the following Stacktrace:

05-24 09:32:48.945      893-893/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service com.example.RemoteService: java.lang.ClassNotFoundException: Didn't find class "com.example.RemoteService" on path: DexPathList[[zip file "/data/app/com.example-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example-1, /system/lib]]
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:2561)
        at android.app.ActivityThread.access$1600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.RemoteService" on path: DexPathList[[zip file "/data/app/com.example-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example-1, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:2558)
        at android.app.ActivityThread.access$1600(ActivityThread.java:141
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338)
...

This is caused by the simple fact, that RemoteController and therefore the implemented interface RemoteController.OnClientUpdateListener is not available on API18, but NotificationListenerService is. The intent is not being sent on every device

What I've tried so far

  • Put another layer of abstraction on it and build a simple "if (API>=19)" check. I tried to do this by using a second empty NotificationListenerService which would just work as a kind of callthrough Service. I would put that second Service in my AndroidManifest, so that it receives the intent instead, and then I'd just put the "if (API>=19)" check in its onCreate and start my actual RemoteService from there. The result is, that my App doesn't crash any more (yay). Unfortunately it also doesn't work in API19 Kitkat any more :(. I think that's because I have to register my RemoteService as a listener in my AndroidManifest directly, or otherwise it won't receive anything.

Code snippets

RemoteService.java:

public class RemoteService extends NotificationListenerService
    implements RemoteController.OnClientUpdateListener {
...
}

AndroidManifest.xml:

<manifest ...>
    <application ...>
        <service android:name="com.example.RemoteService"
             android:label="@string/app_name"
             android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
    </application>
</manifest>

解决方案

The solution

Put a bool in an xml file in values and values-v19. Set it to false in values and to true in values-v19. Now set the NotificationListenerService's "android:enabled" tag to this bool.

./values/bool_switches.xml:

<resources>
    <bool name="remotecontrollerservice_enabled">false</bool>
</resources>

./values-v19/bool_switches.xml:

<resources>
    <bool name="remotecontrollerservice_enabled">true</bool>
</resources>

./AndroidManifest.xml:

...
<service android:name=".services.RemoteControllerService"
         android:label="@string/scrobblingservice_string"
         android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
         android:enabled="@bool/remotecontrollerservice_enabled">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService"/>
    </intent-filter>
</service>
...

这篇关于NotificationListenerService与API18 RemoteController.OnClientUpdateListener崩溃(4.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆