How To Launch An App From Another App In Android
I am developing an app in which i want to launch any application installed on my device. I have tried the following code. Button bClock = (Button) findViewById(R.id.button1); Strin
Solution 1:
Use the Following Function
publicvoidstartNewActivity(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null) {
// We found the activity now start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
// Bring user to the market or let them choose an app?
intent = newIntent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + packageName));
context.startActivity(intent);
}
}
Solution 2:
For your code correction please make String app="com.whatsapp";
a final variable or you can use package name directly like following
You should use the function of the package manager.
Context ctx=this; // or you can replace **'this'** with your **ActivityName.this**try {
Intenti= ctx.getPackageManager().getLaunchIntentForPackage("com.whatsapp");
ctx.startActivity(i);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
}
Solution 3:
If so then make it final
finalString app="com.whatsapp";
OR
Declare it as global variable of class like
publicclassMyClass{
String app="com.whatsapp";
//Other methods
}
Post a Comment for "How To Launch An App From Another App In Android"