我正在尝试编写一个在启动时启动的应用程序。我的接收器工作,我的应用程序启动时启动,但由于ActivityNotFoundException它崩溃。我可能在某个地方弄明白了。请帮忙看看。
这是接收器代码,LocationLock.class只是用来锁定手机的东西。
public class MyStartupIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Intent myStarterIntent = new Intent(context, LocationLock.class);
myStarterIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationLock$Controller"
android:label="@string/app_name_controller">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".LocationLock"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<receiver android:name=".MyStartupIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
以下是错误:
05-03 02:54:55.301: ERROR/AndroidRuntime(264): FATAL EXCEPTION: main
05-03 02:54:55.301: ERROR/AndroidRuntime(264): java.lang.RuntimeException: Unable to start receiver org.example.locationlock.MyStartupIntentReceiver: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.locationlock/org.example.locationlock.LocationLock}; have you declared this activity in your AndroidManifest.xml?
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.os.Looper.loop(Looper.java:123)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at java.lang.reflect.Method.invoke(Method.java:521)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at dalvik.system.NativeStart.main(Native Method)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.locationlock/org.example.locationlock.LocationLock}; have you declared this activity in your AndroidManifest.xml?
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.ContextImpl.startActivity(ContextImpl.java:622)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at org.example.locationlock.MyStartupIntentReceiver.onReceive(MyStartupIntentReceiver.java:12)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
05-03 02:54:55.301: ERROR/AndroidRuntime(264): ... 10 more
也许我不能一个接一个地列出两个接收器,但MyStartupIntentReceiver没有与之关联的活动。
非常感谢任何帮助!
答案 0 :(得分:1)
您正在尝试启动接收器类:Intent myStarterIntent = new Intent(context, LocationLock.class);
,因为在清单文件中它被声明为接收器类:<receiver android:name=".LocationLock"
所以将此声明修改为activiy:
<activity
android:name=".LocationLock"
android:windowSoftInputMode="stateVisible|adjustResize" >
</activity>
答案 1 :(得分:0)
您是否尝试将其定义为清单中的活动?
例如,在您的部分中:
<activity android:name=".MyStartupIntentReceiver"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:noHistory="true" />