From one of my apps, I\'m trying to launch another. I want to use an explicit intent.
ComponentName cn = new ComponentName(\"com.myOtherApp\", \"OtherAppActi
Try something like this...
In the manifest for 'myOtherApp' use an intent filter for 'OtherAppActivity' with a company specific intent, example...
<activity
android:name=".OtherAppActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.mycompany.DO_SOMETHING" />
</intent-filter>
</activity>
Then, in the 'calling' app, use...
Intent intent = new Intent();
intent.setAction("com.mycompany.DO_SOMETHING");
context.startActivity(intent);
I had this problem and searched for hours looking for a solution. Finally found it: http://www.krvarma.com/2010/08/launching-external-applications-in-android. That link shows how to use the package manager to launch any application for which you have simply the package name:
PackageManager pm = this.getPackageManager();
try
{
Intent it = pm.getLaunchIntentForPackage(sName);
if (null != it)
this.startActivity(it);
}
catch (ActivityNotFoundException e)
{
}
In addition to @Sogger answer thing to remember is if you receiver class is com.myOtherApp.receiver.OtherAppActivity and package mentioned in AndroidManifest is com.myOtherApp your code will be
ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.receiver.OtherAppActivity");
You need to specify the fully qualified class name in the second parameter of new ComponentName like this:
ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.OtherAppActivity");
I think this is because the package name in the manifest and the activity name don't necessarily have to have the same package path, so the new ComponentName call doesn't infer the class name second parameter is prefixed by the package name first parameter.
Create the intent as action.Main and add the launcher category to it:
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
As of API23, you could use the method ComponentName.createRelative(String pkg, String cls) and do:
ComponentName cn = new ComponentName(ComponentName.createRelative("com.myOtherApp", ".OtherAppActivity"));
Intent intent = new Intent();
intent.setComponent(cn);
context.startActivity(intent);
This way you can create a ComponentName object using a relative class path. Mind the dot in the start of the class path. It is necessary to indicate that the method should treat the second argument as a relative path. Just as @Sogger mentioned, the ComponentName constructor constraints the class parameter to be an absolute path.
Note also that by this manner, you are using explicit intents and you don't have to insert any additional intent filters to the destination activity.