我想在设备启动完成后运行我的应用程序。 为此,我发现在互联网上的一些有用的代码:Android自动启动活动(应用程序启动两次)
public class MyBootCompleteReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent pushIntent = new Intent(context, MainActivity.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(pushIntent);
}
}
}
而且Manifest条目:
<receiver android:name="MyBootCompleteReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
现在,如果我启动我的设备的应用程序似乎启动两次。 然后我尝试用“REORDER_TO_FRONT”替换“NEW_TASK”标志(我也在互联网上阅读过),但是如果我这样做了,我的应用程序会在启动时崩溃。 此外,如果我删除所有标志,它也崩溃。
希望你能帮助!
它为什么会崩溃?任何错误日志? – 2012-02-13 13:23:46
不,不幸的是(因为我的eclipse没有在启动后记录)。 解锁屏幕后,出现典型的黑色对话框(应用程序意外崩溃 - >强制关闭)。 – Chris 2012-02-13 13:47:10