Android意图启动应用程序的主要活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11040851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Intent to start Main activity of application
提问by Abhishek
I am trying to start the main activity from inside a BroadcastReceiver. I dont want to supply the activity class name but to use the action and category for android to figure out the main activity.
我正在尝试从 BroadcastReceiver 内部启动主要活动。我不想提供活动类名称,而是使用 android 的动作和类别来找出主要活动。
It doesnt seem to work.
它似乎不起作用。
Sending Code:
发送代码:
Intent startIntent = new Intent();
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startIntent.setAction(Intent.ACTION_MAIN);
startIntent.setPackage(context.getPackageName());
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(startIntent);
I get this error:
我收到此错误:
Caused bt: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.xyz.abc (has extras) }
导致 bt: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.xyz.abc (有额外的) }
Any ideas?
有任何想法吗?
采纳答案by Tal Kanel
this is not the right way to startActivity.
try this code instead:
这不是 startActivity 的正确方法。
试试这个代码:
Intent startIntent = new Intent(context, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
回答by TienLuong
Copy from another topic:
从另一个主题复制:
This works since API Level 3 (Android 1.5):
这从 API 级别 3 (Android 1.5) 开始工作:
private void startMainActivity(Context context) throws NameNotFoundException {
PackageManager pm = context.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
context.startActivity(intent);
}
回答by Arnab Saha
Even I had been trying to launch the MainActivity via a library Activity.
甚至我一直在尝试通过库活动启动 MainActivity。
And this worked for me:
这对我有用:
Intent startIntent = new Intent();
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startIntent.setPackage(getApplicationContext().getPackageName());
getApplicationContext().startActivity(startIntent);
Make sure you add the activity in your library's manifest!
确保在库的清单中添加活动!
回答by cantona_7
Even though it is too late. Might be useful for somone in the future. This helped me.
尽管为时已晚。将来可能对某人有用。这对我有帮助。
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

