Reputation: 7916
I want to start another application's Activity
from my Activity
, but all info, that I have is only the name of that application's package. I do following:
Intent intent = new Intent();
intent.setPackage(anotherApplicationPackageName);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
As I understand correctly, using this API, Android will look for an Activity
, which can handle this Intent
inside the given package, but I always get ActivityNotFoundException
.
The question is What am I doing wrong?
Upvotes: 2
Views: 6461
Reputation: 121
As you know the package name of the other app and can't add any intent filter to the destination app, then you should use this to launch the other app,
Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(anotherApplicationPackageName);
startActivity(intent);
It works fine.
Upvotes: 1
Reputation: 16832
I had a similar problem for Intent.ACTION_SEND
. Probably this code will be useful for someone:
/** Knowing intent and package name, set the component name */
public static Intent setComponentByPackageName(Context context, String packageName, Intent intent0) {
Intent intent = new Intent(intent0);
if (null == intent.getType()) {
intent.setType("text/plain");
}
PackageManager pm = context.getPackageManager();
List<ResolveInfo> rList = pm.queryIntentActivities(intent, 0);
// possible flags:
// PackageManager.MATCH_DEFAULT_ONLY PackageManager.GET_RESOLVED_FILTER
for (ResolveInfo ri : rList) {
if (packageName.equals(ri.activityInfo.packageName)) {
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
return intent;
}
}
return null;
}
In the course of trying to make it work (the ResolveInfo list was empty), I found out that a setType() was missing (you don't need to setType() and setPackage() if you setComponent()), so in fact the following is enough:
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.xxxxxx");
// ...
intent.putExtra(Intent.EXTRA_TEXT, text);
// ...
startActivityForResult(intent, ...);
Upvotes: 0
Reputation: 5532
PackageManager pmi = getPackageManager();
Intent intent = null;
intent = pmi.getLaunchIntentForPackage(packageNameToLaunch);
if (intent != null){
startActivity(intent);
}
Upvotes: 8
Reputation: 773
You have to declare another activity's intent filter with category 'Intent.CATEGORY_DEFAULT' and create a custom action and call it using that category and action, setting the package in the intent only reduces its scope.
Upvotes: 1