启动应用程序知道包名称

bar*_*art 72 android

有人能告诉我如何只知道它的包名来启动新的应用程序吗?我没有关于什么活动是主要活动的信息.

pat*_*ric 145

只需使用以下两行,即可启动package name已知的已安装应用程序:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");
startActivity( launchIntent );
Run Code Online (Sandbox Code Playgroud)

如果您不知道要启动的应用程序包名称,请尝试一下

PackageManager pm;
pm = getPackageManager();
//  get a list of installed apps.
packages = pm.getInstalledApplications(0);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此链接使用包管理器


Com*_*are 91

尝试使用PackageManagergetLaunchIntentForPackage()


Anu*_*oob 16

您可以通过PackageManager课程获得启动意图:

PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.package");
context.startActivity(launchIntent);
Run Code Online (Sandbox Code Playgroud)

请注意,getLaunchIntentForPackage如果找不到包,则返回null.所以你可能想要添加一个空检查:

if (launchIntent != null) {
    context.startActivity(launchIntent);
} else {
    Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)