我已经在这里待了很长时间了,我不知道为什么它不起作用。我试图在设备启动时使用BroadcastReceiver启动服务。当设备启动时,它会崩溃并询问我是否已在AndroidManifest中注册了该活动。
我正在使用Genymotion模拟器,并尝试删除应用程序以及在Eclipse中清理项目。我也尝试将全名添加到android:name" com.gordon.status.service.StartBackgroundSyncService,"那也不起作用。
BroadcastReceiver:
public class AutostartReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, StartBackgroundSyncService.class);
context.startService(i);
}
}
清单:
<service
android:name="service.StartBackgroundSyncService"
android:enabled="true"
android:exported="false"/>
<service
android:name="service.LinkedInBackgroundService"
android:enabled="true"/>
<receiver android:name="receiver.AutostartReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" /> <!-- For some HTC devices -->
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
服务(设备启动时应显示Hello)
public class StartBackgroundSyncService extends Service
{
@Override
public void onCreate()
{
super.onCreate();
System.out.println("HELLO=========================================================");
System.out.println("==============================================================");
}
public int startCommand(Intent intent, int flags, int startId)
{
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
Logcat:
10-17 20:48:07.438: E/AndroidRuntime(1147): java.lang.RuntimeException: Unable to start receiver receiver.AutostartReceiver: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gordon.status/service.StartBackgroundSyncService}; have you declared this activity in your AndroidManifest.xml?
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.os.Looper.loop(Looper.java:137)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-17 20:48:07.438: E/AndroidRuntime(1147): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 20:48:07.438: E/AndroidRuntime(1147): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 20:48:07.438: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 20:48:07.438: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-17 20:48:07.438: E/AndroidRuntime(1147): at dalvik.system.NativeStart.main(Native Method)
10-17 20:48:07.438: E/AndroidRuntime(1147): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gordon.status/service.StartBackgroundSyncService}; have you declared this activity in your AndroidManifest.xml?
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ContextImpl.startActivity(ContextImpl.java:957)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ContextImpl.startActivity(ContextImpl.java:939)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
10-17 20:48:07.438: E/AndroidRuntime(1147): at receiver.AutostartReceiver.onReceive(AutostartReceiver.java:16)
10-17 20:48:07.438: E/AndroidRuntime(1147): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)
答案 0 :(得分:0)
某处,而不是您在此处的代码中,您在指向startActivity()
的{{1}}上呼叫Intent
。由于StartBackgroundSyncService
不是活动,因此失败。在这样的StartBackgroundSyncService
上找到您要拨打startActivity()
的位置,并将其更改为指向您应用中的实际活动。