在我的Android項目,我有一個Service
類:WakefulBroadcastReceiver得到拋出java.lang.ClassNotFoundException
public class MyService extends Service{
...
//Defined a WakefulBroadcastReceiver as a inner static class
public static class DeviceRebootReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Do something when user reboot the device
}
}
}
正如你看到的上面,我在MyService
類中定義的WakefulBroadcastReceiver
爲內部靜態類。當用戶重新啓動他/她的設備時,該接收器接收廣播。
我的AndroidManifest.xml是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.project"
android:versionCode="1"
android:versionName="2.1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
...>
<activity ...> ... </activity>
<service android:name="com.my.project.MyService"
android:exported="false"/>
<receiver
android:name="com.my.project.MyService$DeviceRebootReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
但是當我運行重啓設備後,我的應用程序&,我logcat的顯示我的錯誤:
Unable to instantiate receiver com.my.project.MyService$DeviceRebootReceiver:
java.lang.ClassNotFoundException: com.my.project.MyService$DeviceRebootReceiver in loader dalvik.system.PathClassLoader[/data/app/com.my.project-1.apk]
爲什麼我的接收機類是不能夠被系統加載?
=====更新======
我也試圖讓我的DeviceRebootReceiver
爲單獨類(DeviceRebootReceiver.java),並提出了AndroidManifest.xml
改爲:
<application...>
<receiver
android:name="com.my.project.DeviceRebootReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
但我仍然得到了logcat中類似的錯誤:
java.lang.RuntimeException: Unable to instantiate receiver com.my.project.DeviceRebootReceiver: java.lang.ClassNotFoundException
是您的服務在重啓設備時運行嗎?可能它;因爲你的服務在重啓時死了,所以子類無法到達,沒有發現異常! – Saqib
@Saqib,我也試圖把我的DeviceRebootReceiver放在它自己的類文件中,但是類似的錯誤仍然在增加。請檢查我的更新。 –
嗯,你有沒有嘗試加入'android:permission =「android.permission.RECEIVE_BOOT_COMPLETED」' – Saqib