How do I programmatically launch a specific application?

后端 未结 10 642
余生分开走
余生分开走 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:18

    in oncreate method call => openApp(); method

    private void openApp() {
        String packageName = "com.google.android.gm";
        if (isAppInstalled(activity, packageName))
            startActivity(getPackageManager().getLaunchIntentForPackage(packageName));
        else Toast.makeText(activity, "App not installed", Toast.LENGTH_SHORT).show();
    }
    
    public static boolean isAppInstalled(Activity activity, String packageName) {
        PackageManager pm = activity.getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-29 04:19

    You should use the function of the package manager.

    Context ctx=this; // or you can replace **'this'** with your **ActivityName.this**
    try {
    Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
    ctx.startActivity(i);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
    }
    
    0 讨论(0)
  • 2020-11-29 04:30

    You use the package name / class directly, for example to create a new intent to call the twidroid program you'd use the followinglink text:

     Intent intent = new Intent("com.twidroid.SendTweet");
    

    You'd probably want to put a try/catch around for a ActivityNotFoundException for when the application is not installed.

    0 讨论(0)
  • 2020-11-29 04:34
    Intent intent = new Intent();    
    intent.setClassName("package.name", "package.name.LauncherActivityName");
    startActivityForResult(intent,REQUEST_CODE);
    
    0 讨论(0)
  • 2020-11-29 04:35

    What i do to open apps is this

    private void _LaunchApp(final String _Pack) {
        Intent launchi = new Intent(Intent.ACTION_VIEW);
        launchi.setData(Uri.parse("android-app://".concat(_Pack)));
        startActivity(launchi);
    }
    

    and the usage

    _LaunchApp("your.package.here");
    
    0 讨论(0)
  • 2020-11-29 04:37

    The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too. - don't forget!

    0 讨论(0)
提交回复
热议问题