Android 自动启动 Activity(应用程序启动两次)
我想在设备启动完成后运行我的应用程序。 为此,我在互联网上找到了一些有用的代码:
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);
}
}
}
和清单条目:
<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”标志(我也在互联网上读过此内容),但如果这样做,我的应用程序将在启动时崩溃。 另外,如果我删除所有标志,它也会崩溃。
希望您能帮忙!
I would like to run my application when the boot up of my device has finished.
For this I have found some helpful code in the internet:
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);
}
}
}
And the Manifest entry:
<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>
Now if I boot up my device the application seems to start up twice.
Then I tried to replace the "NEW_TASK" Flag with the "REORDER_TO_FRONT" (I have also read this in the internet) but if i do so, my application crashes on boot up.
Also if I remove all flags, it crashes too.
Hope you can help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以......
经过长时间的测试,我发现问题应该是我的物理平板电脑(华硕 eee pad transfortem android 版本 3.2.1)。
因为我已经在模拟器(android 版本 3.2)中尝试过,并且我发布的源代码(在此线程的顶部)运行得很好。
应用程序没有启动两次,也没有错误。
即使您删除“pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);”代码它有效。
所以看来我必须等待更新(也许在android 4.0上)。
我希望这对有同样问题的人有所帮助。
So...
after long long testing I found out that the problem should be my physical tablet (asus eee pad transfortem android version 3.2.1).
Because I have tried it in the emulator (android version 3.2) and there the source code I posted (on the top of this thread) worked quite well.
No start up twice of the app and no errors.
Even if you remove the "pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);" code it works.
So it seems that I have to wait for an update (maybe on android 4.0).
I hope this helps somebody, who has the same problem.
在清单文件的活动中使用启动完成意图过滤器。
我希望它有帮助...
Use the boot complete intent filter in you activity in you manifest file.
I hope it helps...