所以我正在尝试自定义通过Intent.ACTION_SEND显示的应用程序,除了WeChat和UrlToPDF之外,它的效果非常好。它不是“微信”,而是显示为“Android-System”。如果我点击它,它会打开另一个意图,其中wechat-app显示正确,但没有其他应用程序。我做错了什么?
以下是代码:
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, forecastStr+" #SunshineApp");
final PackageManager pm = getActivity().getPackageManager();
final List<ResolveInfo> resInfo = pm.queryIntentActivities(shareIntent, 0);
ArrayList <String> packageAllowed = new ArrayList();
packageAllowed.add("com.twitter.android");
packageAllowed.add("com.facebook.katana");
packageAllowed.add("com.google.android.gm");
packageAllowed.add("com.whatsapp");
packageAllowed.add("com.evernote");
packageAllowed.add("ch.threema.app");
packageAllowed.add("com.skype.raider");
packageAllowed.add("com.tencent.mm");
packageAllowed.add("com.tencent.mm.ui.tools.ShareToTimeLineUI");
ArrayList <String> packageSum = new ArrayList();
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
if(packageAllowed.contains(packageName)){
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.setPackage(packageName);
targetedShareIntents.add(intent);
}
}
int size = targetedShareIntents.size();
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size()-1), "Share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
答案 0 :(得分:4)
尝试另外设置组件名称:
if(packageAllowed.contains(packageName)){
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
// This is new!!!
intent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
intent.setPackage(packageName);
targetedShareIntents.add(intent);
}