从Android上的其他应用程序启动应用程序

Bas*_*ian 458 android android-intent

我想从我的Android应用程序中启动已安装的软件包.我假设有可能使用意图,但我找不到这样做的方法.有链接,在哪里可以找到信息?

and*_*dep 683

如果您不知道主要活动,则可以使用包名称来启动应用程序.

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
if (launchIntent != null) { 
    startActivity(launchIntent);//null pointer check in case package name was not found
}
Run Code Online (Sandbox Code Playgroud)

  • 它启动一个新的Intent,如何恢复后台的应用程序? (21认同)
  • 但对我来说,这显示空指针异常 (7认同)
  • 对于 android 版本 10+,您还需要在 AndroidManifest.xml 文件中添加 <queries> 标记,否则 getLaunchIntentForPackage() 将返回 null,即 <queries> <package android:name="OtherApplicationID" /> </queries> (7认同)
  • 任何理由为什么这不起作用?我至少没有让它工作. (5认同)
  • @andep:当我在自己创建的两个应用程序之间进行测试时,这对我很有用.一旦我知道包名称这将始终有效,或者是否有办法阻止某人启动您的应用程序(在maniefest或某处)? (3认同)
  • @Leonard:我的第一印象是它必须始终有效,因为包名是公开的,所以任何应用都可以读出它们.从您的应用程序我认为您无法确定它的调用位置,但您的应用程序可以确定无法通过主要活动仅通过服务进行调用. (2认同)
  • 是的,这可以返回 null。“当前实现首先查找类别`CATEGORY_INFO`中的主要活动,然后查找类别`CATEGORY_LAUNCHER`中的主要活动。**如果两者都没有找到,则返回null。**” (2认同)

Jar*_*ows 228

我知道这已经得到了解答,但这是我实现类似的方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

更好的是,方法如下:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

删除重复的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 在将Intent发布到Facebook或Twitter个人资料时遇到问题.他们在我的应用程序内部打开,而不是作为一项新活动.添加固定的FLAG_ACTIVITY_NEW_TASK.谢谢! (8认同)
  • 没问题!我遇到了一些非常相似的问题 (4认同)

Bas*_*ian 145

我找到了解决方案.在应用程序的清单文件中,我找到了包名:com.package.address和我要启动的主要活动的名称:MainActivity以下代码启动此应用程序:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

  • 我在你的Manifest.xml中声明了活动的例外情况 (6认同)

smo*_*fun 15

这是我从我的应用程序启动条/ QR码扫描程序的例子,如果有人发现它有用

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");

try 
{
    startActivityForResult(intent, SCAN_REQUEST_CODE);
} 
catch (ActivityNotFoundException e) 
{
    //implement prompt dialog asking user to download the package
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this);
    downloadDialog.setTitle(stringTitle);
    downloadDialog.setMessage(stringMessage);
    downloadDialog.setPositiveButton("yes",
            new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialogInterface, int i) 
                {
                    Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    try
                    {
                        myActivity.this.startActivity(intent);
                    }
                    catch (ActivityNotFoundException e)
                    {
                        Dialogs.this.showAlert("ERROR", "Google Play Market not found!");
                    }
                }
            });
    downloadDialog.setNegativeButton("no",
            new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int i) 
                {
                    dialog.dismiss();
                }
            });
    downloadDialog.show();
}
Run Code Online (Sandbox Code Playgroud)


Aha*_*kat 15

// in onCreate method
String appName = "Gmail";
String packageName = "com.google.android.gm";
openApp(context, appName, packageName);

public static void openApp(Context context, String appName, String packageName) {
    if (isAppInstalled(context, packageName))
        if (isAppEnabled(context, packageName))
            context.startActivity(context.getPackageManager().getLaunchIntentForPackage(packageName));
        else Toast.makeText(context, appName + " app is not enabled.", Toast.LENGTH_SHORT).show();
    else Toast.makeText(context, appName + " app is not installed.", Toast.LENGTH_SHORT).show();
}

private static boolean isAppInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException ignored) {
    }
    return false;
}

private static boolean isAppEnabled(Context context, String packageName) {
    boolean appStatus = false;
    try {
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packageName, 0);
        if (ai != null) {
            appStatus = ai.enabled;
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return appStatus;
}
Run Code Online (Sandbox Code Playgroud)


Joa*_*oao 9

从 API 30 (Android 11) 开始,您可以使用 launchIntentForPackage 接收 nullpointerException

val launchIntent: Intent? = activity.packageManager.getLaunchIntentForPackage("com.google.android.gm")
startActivity(launchIntent) 
Run Code Online (Sandbox Code Playgroud)

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

<queries>
    <package android:name="com.google.android.gm" />
</queries>
Run Code Online (Sandbox Code Playgroud)

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

以及中等文章 https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9


may*_*513 8

虽然回答得很好,但是有一个非常简单的实现可以处理是否未安装该应用程序。我这样做

try{
    startActivity(getPackageManager().getLaunchIntentForPackage("applicationId"));
} catch (PackageManager.NameNotFoundException e) {
    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://play.google.com/store/apps/details?id=" + "applicationId")));
}
Run Code Online (Sandbox Code Playgroud)

用您要打开的包(例如com.google.maps等)替换“ applicationId”。


小智 6

// check for the app if it exist in the phone it will lunch it otherwise, it will search for the app in google play app in the phone and to avoid any crash, if no google play app installed in the phone, it will search for the app in the google play store using the browser : 

 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);

        }

    }
Run Code Online (Sandbox Code Playgroud)


Vig*_* KM 6

如果您想打开另一个应用程序的特定活动,我们可以使用它.

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try 
{
    startActivity(intent)
}catch(ActivityNotFoundException e){
    Toast.makeText(context,"Activity Not Found",Toast.LENGTH_SHORT).show()
}
Run Code Online (Sandbox Code Playgroud)

如果您必须需要其他应用程序,而不是显示Toast,则可以显示对话框.使用对话框,您可以将用户带到Play-Store以下载所需的应用程序.


Pha*_*vij 6

可以Intent.setClassName根据文档使用来启动应用程序的活动。

一个例子:

val activityName = "com.google.android.apps.muzei.MuzeiActivity" // target activity name
val packageName = "net.nurik.roman.muzei" // target package's name
val intent = Intent().setClassName(packageName, activityName)
startActivity(intent)
Run Code Online (Sandbox Code Playgroud)

要在当前应用程序外部打开它,请在启动意图之前添加此标志。

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Run Code Online (Sandbox Code Playgroud)

相关答案在这里


War*_*ith 2

如果您知道已安装的软件包所反应的数据和操作,您只需在启动之前将这些信息添加到您的意图实例中即可。

如果您有权访问其他应用程序的 AndroidManifest,则可以在那里看到所有需要的信息。

  • 我发现错误:设置组件时,必须命名完全限定的类名而不仅仅是类:intent.setComponent(new ComponentName("com.package","com.package.MainActivity")) 而不是intent .setComponent(new ComponentName("com.package",".MainActivity")) (5认同)