我想实施新的RemoteController API,它已经introduced with API19 (Kitkat,4.4)。 API要求我实现一个从NotificationListenerService扩展的类(与API18一起引入)并实现RemoteController.OnClientUpdateListener接口。 (可以找到有关如何实现所有这些的更多信息here,可以找到一个非常有用的示例项目here)
我的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)
...
这是由一个简单的事实导致的RemoteController,因此实施的接口RemoteController.OnClientUpdateListener在API18上不可用,但是NotificationListenerService。意图不是在每台设备上发送的
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>
答案 0 :(得分:5)
将bool放在xml文件中的值和值-v19中。在值中将其设置为false,在值-v19中将其设置为true。现在设置NotificationListenerService&#34; android:enabled&#34;标记为此bool。
<强> ./值/ bool_switches.xml:强>
<resources>
<bool name="remotecontrollerservice_enabled">false</bool>
</resources>
<强> ./值-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>
...
答案 1 :(得分:1)
只需使用android:enabled =“false”禁用该服务,然后在Application.onCreate中启用它,如果设备运行的是KitKat或更新版本。