Android 启动应用程序知道包名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3422758/
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
start application knowing package name
提问by bart
Can someone tell me how to start new application knowing only its package name? I do not have information about what activity is the main one.
有人能告诉我如何只知道它的包名来启动新应用程序吗?我没有关于什么活动是主要活动的信息。
回答by patric_cena
Just use these following two lines, so you can launch any installed application whose package nameis known:
只需使用以下两行,即可启动任何package name已知的已安装应用程序:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");
startActivity( launchIntent );
If you don't know the package name of application that you wanted to launch then try your hand on
如果您不知道要启动的应用程序的包名称,请尝试
PackageManager pm;
pm = getPackageManager();
//  get a list of installed apps.
packages = pm.getInstalledApplications(0);
For more info refer this link Using Package Manager
有关更多信息,请参阅此链接使用包管理器
回答by CommonsWare
Try using PackageManagerand getLaunchIntentForPackage()
尝试使用PackageManager和getLaunchIntentForPackage()
回答by Anubian Noob
You can get the launch intent through the PackageManagerclass:
您可以通过PackageManager该类获取启动意图:
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.package");
context.startActivity(launchIntent);
Note that getLaunchIntentForPackagereturns null if the package isn't found. So you might want to add a null check:
请注意,getLaunchIntentForPackage如果未找到包,则返回 null。所以你可能想添加一个空检查:
if (launchIntent != null) {
    context.startActivity(launchIntent);
} else {
    Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
}
回答by Siddhi Mandodi
Intent intent;                                        
PackageManager pm = getActivity().getPackageManager();
intent = pm.getLaunchIntentForPackage("com.package.name");                       
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

