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
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;
}
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);
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");
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!