我想在Intentchooser中只展示Messaging app和Whatsapp应用程序。以下代码与其他一些设备(如moto e,g等)正常工作。但在其他一些设备中,它现在显示默认的消息传递应用程序。这是我的代码。请帮帮我。
private void shareTheApp() {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
if (packageName.contains("com.whatsapp")) {
emailIntent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
emailIntent.setType("text/plain");
emailIntent.setPackage(packageName);
}
if (packageName.contains("mms")) {
isMmsAvailable = true;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if (packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
if(!isMmsAvailable) {
isMmsAvailable = false;
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:"));
smsIntent.putExtra("sms_body", resources.getString(R.string.share_text));
intentList.add(new LabeledIntent(smsIntent, "", "Messaging", R.drawable.ic_launcher));
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
Intent openInChooser = Intent.createChooser(emailIntent, getResources().getString(R.string.openchooser_tittle));
openInChooser.addCategory(Intent.CATEGORY_DEFAULT);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
答案 0 :(得分:0)
是的,我在代码下面解决了这个问题:)。
Resources resources = getResources();
List<Intent> selectedIntents = new ArrayList<Intent>();
Intent shareIntent=new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);
if(!resInfos.isEmpty()){
for(ResolveInfo resInfo : resInfos) {
String packageName=resInfo.activityInfo.packageName;
if(packageName.contains("com.whatsapp")){
Intent intent=new Intent();
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
intent.setPackage(packageName);
selectedIntents.add(intent);
}
}
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(sendIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("mms")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if (packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
selectedIntents.add(intent);
}
}
}
if(!selectedIntents.isEmpty()){
Intent chooserIntent = null;
chooserIntent = Intent.createChooser(selectedIntents.get(0), resources.getString(R.string.openchooser_tittle));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, selectedIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
我们需要同时调用ACTION_SEND和ACTION_VIEW。现在它将在两种消息传递应用程序的意图选择器中显示。希望它对其他人有用。