Monday, November 5, 2012

Launching other application using code in Android


Here is how to open applications using code

See below example.

Suppose your packagename = com.abc.xyz
Activity name = MainActivity
Than your code is like below example.

final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.abc.xyz", "com.abc.xyz.MainActivity");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);

Detail Description:



To open other people's application, you need to make sure that in their manifest file, the author specify the class to have the android.intent.action.MAIN intent-filter added to them.
final Intent intent = new Intent(Intent.ACTION_MAIN, null);

We then add category that this new intent will be launching something



intent.addCategory(Intent.CATEGORY_LAUNCHER);
Then we get identify the application we need to open by using ComponentName, final ComponentName cn = new ComponentName("PACKAGE_NAME", "ACTIVITY_NAME"); After we identify the component we want, we set it to our intent



intent.setComponent(cn);
We then tell the intent that open opening this one make it as a new task



intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Then finally start our intent



startActivity( intent);

No comments:

Post a Comment