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 to go directly to a particular app. Hope this makes sense.
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 to go directly to a particular app. Hope this makes sense.
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
}
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.
Intent intent = new Intent();
intent.setClassName("package.name", "package.name.LauncherActivityName");
startActivityForResult(intent,REQUEST_CODE);
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());
}
From SDK 30 (Android 11) you need to add to your manifest:
<queries>
<package android:name="com.app.you.need" />
</queries>
and then this:
Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.app.you.need");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
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;
}
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!
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);
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);
This is what I use to open apps
private void launchApp(String pack) {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse("android-app://".concat(pack)));
startActivity(launchIntent);
}
And this is how to use it
launchApp("your.package.here");
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);