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是.意图不是在每台设备上发送
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)
将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)
| 归档时间: |
|
| 查看次数: |
1230 次 |
| 最近记录: |