开始应用程序,知道包名称。

88

如何使用应用程序包名启动新应用程序?我不知道哪个活动是主要的。

6个回答

168

只需使用以下两行代码,就可以启动已知包名的任意安装应用程序:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");
startActivity( launchIntent );
如果你不知道想要启动的应用程序的包名,那么可以尝试使用以下方法:

如果你不知道想要启动的应用程序的包名,那么可以尝试使用以下方法:

PackageManager pm;
pm = getPackageManager();
//  get a list of installed apps.
packages = pm.getInstalledApplications(0);

更多信息,请参考此链接:使用包管理器


1
这会启动一个全新的实例吗?我想要启动现有的实例。我该如何做到这一点? - JohnyTex
@hohny,你解决了你的问题吗? - Menna-Allah Sami
@JohnyTex 我知道有点晚了,但你可以尝试使用 LaunchIntent.setFlags("Intent.FLAG_ACTIVITY_NEW_TASK");LaunchIntent.setFlags("Intent.FLAG_ACTIVITY_SINGLE_TOP"); - KISHORE_ZE
在 Android 11 的软件包可见性更改后,这仍然是可靠的解决方案吗? - argenkiwi

104

先生,为什么我无法通过获取启动器的包名来启动它,它一直报 java.lang.NullPointer 异常? - TechArcSri
我会添加标志(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED),以确保应用程序状态与先前打开时相同。 - JacksOnF1re
当我尝试将Extras传递到目标应用程序时,遇到了一个问题。只有在目标应用程序的进程不存在时,才能获取Extras,否则它将什么都得不到。 - Allen Vork
1
在 Android 11 上的包可见性更改后,这仍然是一个可靠的解决方案吗? - argenkiwi
1
@argenkiwi:我很少使用这种方法。 我期望它要么可以直接工作,要么您需要为MAIN/LAUNCHER Intent结构使用<queries>。 据我所知,它并没有完全损坏。 - CommonsWare
显示剩余3条评论

23
您可以通过PackageManager类获得启动意图:
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.package");
context.startActivity(launchIntent);
请注意,如果未找到包,则getLaunchIntentForPackage将返回null。因此,您可能需要添加空值检查:

请注意,如果未找到包,则getLaunchIntentForPackage将返回null。因此,您可能需要添加空值检查:

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

这个答案与旧答案几乎没有什么不同,只是它比旧答案新了3年。我不明白为什么会有这么多赞。 - hBrent
但至少他发帖说如果没有包就返回null,我本来想尝试捕获未找到活动异常的。@hBrent - Xenolion

3
Intent intent;                                        
PackageManager pm = getActivity().getPackageManager();

intent = pm.getLaunchIntentForPackage("com.package.name");                       
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

2
    Intent intent = getPackageManager().getLaunchIntentForPackage("app.package.name");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if (intent != null) {
        startActivity(intent);
    } else {
        Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
    }

2
val packageName = "com.google.android.youtube"
var intent = activity!!.packageManager.getLaunchIntentForPackage(packageName)

          if (intent == null) {
            if (intent == null) {
                    intent = try {
                        Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName"))
                    } catch (e: Exception) {
                        Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName"))
                    }
                }
             startActivity(intent)

对于 Android 11 (API 级别 30) 或更高版本,在 AndroidManifest.xml 文件中:

<queries>
    <package android:name="com.google.android.youtube" />
    <package android:name="com.example.app" />
</queries>

或者我们可以允许所有软件包(不推荐)。

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />

参考文献

Android上的包可见性过滤


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接