Android 在启动时从 BroadcastReceiver 崩溃启动应用程序
我尝试启动我的 Android 2.1 Galaxy S 手机,但它崩溃了。
这是我的接收器,如果我注释掉 context.startActivity(i) 我不会崩溃,否则我会在开机时看到它。使用相同 ACTION 从另一个活动启动活动不会导致崩溃。这似乎只是在启动时。
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setAction("DISPLAY_FIRSTPAGE");
context.startActivity(i);
}
}
我在清单中设置了一个接收器,如下所示:
<receiver android:name=".MyBroadcastReceiver">
android:enabled="true" android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I am trying to start my Android 2.1 Galaxy S Phone on boot and it crashes.
Here is my receiver if I comment out context.startActivity(i) I don't get crash otherwise I see it on powerup. startActivity from another activity using same ACTION does not cause crash. This seems to be just on Boot.
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setAction("DISPLAY_FIRSTPAGE");
context.startActivity(i);
}
}
I setup an receiver in the manifest like this:
<receiver android:name=".MyBroadcastReceiver">
android:enabled="true" android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请先查看日志,然后再进行其他操作。在这种情况下,问题(包括原始代码和固定代码)将在日志中的崩溃中得到非常清楚的解释。
Please first look at the log before anything else. In this case the problem (both with your original code and your fixed code) will be pretty clearly explained in the crash in the log.
您显然没有告诉它要启动什么(除非您指定您的活动在清单中处理
DISPLAY_FIRSTPAGE
意图,这不是一个好主意)。尝试一些类似的事情:You're clearly not telling it what to launch (unless you specify that your activity handles
DISPLAY_FIRSTPAGE
intents in the manifest, which wouldn't be a good idea). Try something along the lines of:这对我有用:
This is what worked for me: