下面列出了怎么用android.content.pm.LabeledIntent的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Share with other applications (but tries to put applications with "Translate" in the package
* name in the first place(s).
*/
public static void shareWithTranslate(Activity activity, String text) {
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<>();
for (ResolveInfo ri : resInfo) {
if(ri.activityInfo.packageName.contains("translate")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
intent.putExtra(android.content.Intent.EXTRA_TEXT, text);
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intentList.add(new LabeledIntent(intent, ri.activityInfo.packageName, ri.loadLabel(pm), ri.icon));
}
}
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
Intent openInChooser = Intent.createChooser(sendIntent, "Translate");
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
activity.startActivity(openInChooser);
}
/**
* From: <a href="https://stackoverflow.com/a/12328282">https://stackoverflow.com/a/12328282</a>
*/
public static @Nullable LabeledIntent getLabelintent(@NonNull Context context, @NonNull Intent origIntent, int name, int drawable) {
PackageManager pm = context.getPackageManager();
ComponentName launchName = origIntent.resolveActivity(pm);
if (launchName != null) {
Intent resolved = new Intent();
resolved.setComponent(launchName);
resolved.setData(origIntent.getData());
return new LabeledIntent(resolved, context.getPackageName(), name, drawable);
}
return null;
}