How to launch an Activity from another Application in Android

后端 未结 12 1448
春和景丽
春和景丽 2020-11-21 06:02

I want to launch an installed package from my Android application. I assume that it is possible using intents, but I didn\'t find a way of doing it. Is there a link, where t

相关标签:
12条回答
  • 2020-11-21 06:24

    I found the solution. In the manifest file of the application I found the package name: com.package.address and the name of the main activity which I want to launch: MainActivity The following code starts this application:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-21 06:24

    Here is my example of launching bar/QR code scanner from my app if someone finds it useful

    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();
    }
    
    0 讨论(0)
  • 2020-11-21 06:29

    It is possible to start an app's activity by using Intent.setClassName according to the docs.

    An example:

    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)
    

    To open it outside the current app, add this flag before starting the intent.

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    

    A related answer here

    0 讨论(0)
  • 2020-11-21 06:31

    If you don't know the main activity, then the package name can be used to launch the application.

    Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
    if (launchIntent != null) { 
        startActivity(launchIntent);//null pointer check in case package name was not found
    }
    
    0 讨论(0)
  • 2020-11-21 06:36
    // 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);
    
            }
    
        }
    
    0 讨论(0)
  • 2020-11-21 06:36
    private fun openOtherApp() {
            val sendIntent = packageManager.getLaunchIntentForPackage("org.mab.dhyanaqrscanner")
            startActivity(sendIntent)
            finishAffinity()
        }
    
    0 讨论(0)
提交回复
热议问题