我正在开发一个Android应用程序,它应该在android的启动时启动。
为此,我在AndroidManifest.xml中添加了以下行
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
和
<receiver android:name=".AppBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
我创建了一个新文件(新类):
public class AppBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, MyService.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
}
}
}
但是当我启动我的Android系统时,我说我的应用程序启动但它直接崩溃
如何解决这些问题?
修改
崩溃日志之后:
12-26 16:47:30.791: E/AndroidRuntime(1877): FATAL EXCEPTION: main
12-26 16:47:30.791: E/AndroidRuntime(1877): java.lang.RuntimeException: Unable to instantiate receiver com.example.openafreerdp.receiver.AppBroadcastReceiver: java.lang.ClassNotFoundException: com.example.openafreerdp.receiver.AppBroadcastReceiver
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2337)
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.app.ActivityThread.access$1500(ActivityThread.java:149)
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.os.Looper.loop(Looper.java:153)
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.app.ActivityThread.main(ActivityThread.java:4987)
12-26 16:47:30.791: E/AndroidRuntime(1877): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:47:30.791: E/AndroidRuntime(1877): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 16:47:30.791: E/AndroidRuntime(1877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-26 16:47:30.791: E/AndroidRuntime(1877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-26 16:47:30.791: E/AndroidRuntime(1877): at dalvik.system.NativeStart.main(Native Method)
12-26 16:47:30.791: E/AndroidRuntime(1877): Caused by: java.lang.ClassNotFoundException: com.example.openafreerdp.receiver.AppBroadcastReceiver
12-26 16:47:30.791: E/AndroidRuntime(1877): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-26 16:47:30.791: E/AndroidRuntime(1877): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-26 16:47:30.791: E/AndroidRuntime(1877): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-26 16:47:30.791: E/AndroidRuntime(1877): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2332)
12-26 16:47:30.791: E/AndroidRuntime(1877): ... 10 more
编辑2:清单xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.openafreerdp"
android:versionCode="1"
android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.openafreerdp.MainActivity"
android:excludeFromRecents="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".UnboundedService" />
<receiver
android:name="com.example.openafreerdp.AppBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="AppBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:1)
您的代码中存在拼写错误。尝试替换以下代码
<receiver android:name=".AppBroadcastReceiver">
与
<receiver android:name=".AppBroadCastReceiver">
有关您的信息,触摸屏幕与系统完成启动时启动服务无关。
更新:
<receiver android:name="com.example.openafreerdp.receiver.AppBroadCastReceiver">