带有RemoteController.OnClientUpdateListener的NotificationListenerService在API18(4.3)上崩溃

MrM*_*fen 1 service android android-notifications android-4.4-kitkat

问题

我想实现新的RemoteController API,它已经与API19(Kitkat,4.4)一起引入.API要求我实现一个从NotificationListenerService扩展的类(与API18一起引入)并实现RemoteController.OnClientUpdateListener接口.(关于如何实现这一切的进一步信息可以发现这里和一个非常有益的示例项目,可以发现这里)

在我的API19(Kitkat,4.4)设备和其他所有API <18设备上,一切正常.然而问题是,应用程序立即崩溃API18(Jelly bean,4.3),因为正在发送"android.service.notification.NotificationListenerService"并启动我的RemoteService,然后使用以下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)
...
Run Code Online (Sandbox Code Playgroud)

这是由一个简单的事实造成的,即RemoteController和实现的接口RemoteController.OnClientUpdateListener在API18上不可用,但NotificationListenerService是.意图不是在每台设备上发送

到目前为止我尝试过的

  • 在其上放置另一层抽象并构建一个简单的"if(API> = 19)"检查.我试图通过使用第二个空的NotificationListenerService来做到这一点,它只是作为一种调用服务.我会把第二个服务放在我的AndroidManifest中,以便它接收到意图,然后我只是将"if(API> = 19)"检查放入其onCreate并从那里启动我的实际RemoteService.结果是,我的应用程序不再崩溃(yay).不幸的是它在API19 Kitkat中也不起作用:(.我认为那是因为我必须直接在我的AndroidManifest中注册我的RemoteService作为监听器,否则它将不会收到任何东西.

代码片段

RemoteService.java:

public class RemoteService extends NotificationListenerService
    implements RemoteController.OnClientUpdateListener {
...
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

MrM*_*fen 5

解决方案

将bool放在xml文件中的值和值-v19中.在值中将其设置为false,在值-v19中将其设置为true.现在将NotificationListenerService的"android:enabled"标记设置为此bool.

./values/bool_switches.xml:

<resources>
    <bool name="remotecontrollerservice_enabled">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

./values-v19/bool_switches.xml:

<resources>
    <bool name="remotecontrollerservice_enabled">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

./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>
...
Run Code Online (Sandbox Code Playgroud)