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, ...);