我想从我的Android应用程序启动一个已安装的包。我认为使用意图是可能的,但我没有找到一种方法来做到这一点。有没有链接,在哪里可以找到相关信息?


当前回答

我知道这个问题已经得到了回答,但以下是我如何实现类似的东西:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.name");
if (intent != null) {
    // We found the activity now start the activity
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
} else {
    // Bring user to the market or let them choose an app?
    intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setData(Uri.parse("market://details?id=" + "com.package.name"));
    startActivity(intent);
}

更好的方法是:

public void startNewActivity(Context context, String packageName) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    if (intent != null) {
        // We found the activity now start the activity
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    } else {
        // Bring user to the market or let them choose an app?
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("market://details?id=" + packageName));
        context.startActivity(intent);
    }
}

删除重复代码:

public void startNewActivity(Context context, String packageName) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    if (intent == null) {
        // Bring user to the market or let them choose an app?
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + packageName));
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

其他回答

检查应用程序,避免任何崩溃。如果应用程序在手机中存在,那么它将被启动,否则它将在谷歌Play中搜索。如果手机没有安装谷歌Play app,手机会通过浏览器在谷歌Play Store中搜索:

public void onLunchAnotherApp() {
    final String appPackageName = getApplicationContext().getPackageName();

    Intent intent = getPackageManager().getLaunchIntentForPackage(appPackageName);
    if (intent != null) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    } else {
        onGoToAnotherInAppStore(intent, appPackageName);
    }
}

public void onGoToAnotherInAppStore(Intent intent, String appPackageName) {
    try {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("market://details?id=" + appPackageName));
        startActivity(intent);
    } catch (android.content.ActivityNotFoundException anfe) {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName));
        startActivity(intent);
    }
}

启动新活动的步骤如下:

1.获取包的意图

2.如果意图为空重定向用户到播放商店

3.如果意图不为空,打开活动

public void launchNewActivity(Context context, String packageName) {
    Intent intent = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.CUPCAKE) {
        intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    }
    if (intent == null) {
        try {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse("market://details?id=" + packageName));
            context.startActivity(intent);
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
        }
    } else {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}

从API 30 (Android 11)开始,你可以通过launchIntentForPackage接收nullpointerexception

val launchIntent: Intent? = activity.packageManager.getLaunchIntentForPackage("com.google.android.gm")
startActivity(launchIntent) 

为了避免这种情况,您需要将所需的包添加到清单中

<queries>
    <package android:name="com.google.android.gm" />
</queries>

这里是文档 https://developer.android.com/training/package-visibility

而媒体文章 https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9

我找到了解决办法。在应用程序的清单文件中,我找到了包的名称:com.package.address和我想启动的主活动的名称:MainActivity 下面的代码启动这个应用程序:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);

由于kotlin现在变得非常流行,我认为在kotlin中也提供一个简单的解决方案是合适的。

var launchIntent: Intent? = null
try {
    launchIntent = packageManager.getLaunchIntentForPackage("applicationId")
} catch (ignored: Exception) {
}
if (launchIntent == null) {
    startActivity(Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://play.google.com/store/apps/details?id=" + "applicationId")))
} else {
    startActivity(launchIntent)
}