How do I programmatically launch a specific application?

后端 未结 10 643
余生分开走
余生分开走 2020-11-29 03:43

I want to launch a specif application.

I know how to do Intents but I want to avoid the selection menu if there are multiple apps that can handle the intent, I want

相关标签:
10条回答
  • 2020-11-29 04:37

    I've resolved issue by

    String packageName = "Your package name";
    
    Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
    
    if(intent == null) {
        try {
            // if play store installed, open play store, else open browser 
             intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
        } catch (Exception e) {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
        }
    } 
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-29 04:40

    I use:

      try {
           Intent intent = new Intent();    
           intent.setClassName("package.name", "<your_package_name>");
           startActivity(intent);
        } catch (NameNotFoundException e) {
           Log.e(TAG, e.getMessage());
        }
    

    But like Cami suggested this will work too:

    try {
        Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
        ctx.startActivity(i);
    } catch (NameNotFoundException e) {
         Log.e(TAG, e.getMessage());
    }
    
    0 讨论(0)
  • 2020-11-29 04:40

    Say you want to open Wonder Clock from your app

    URL: https://play.google.com/store/apps/details?id=ganesha.diva.app.wonderclock.free&hl=en_US

    id field/package name is of our interest = ganesha.diva.app.wonderclock.free

    The Package Manager keeps track of the apps installed on the device and maintains the list of the packages. So anything related to installed apps on the device, the Package manager should be consulted.

    The bundle id or package name is unique across globe so it can be used to check the existence of the package/app on the device.

    To Open the Wonder Clock from your app

    • Ask Package Manager, for the launcher activity of the given package name

      Intent intent = context.getPackageManager().getLaunchIntentForPackage("ganesha.diva.app.wonderclock.free");

    • Check if launcher activity exists or not if(intent != null) continue... else app is not installed on the device take user to the URL

    • Add the required flags to the intent intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    • start the activity startActivity(intent);

    0 讨论(0)
  • 2020-11-29 04:42

    Launch app using Intent with ComponentName

    ComponentName cName = new ComponentName("packageName","packagename.yourMainActivity");
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.setComponent(cName);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mContext.startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题