背景
通过使用意图来共享内容是很常见的:
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "msg");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
final Intent chooserIntent = Intent.createChooser(shareIntent, "Share using...");
startActivity(chooserIntent);
甚至可以将收件人添加到相同的共享意图中:
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
shareIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, new String[] { phoneNumber });
问题
问题在于消息本身的内容。在 SMS 上,消息可能要花钱,因此保持简短很重要,以便只发送一条消息而不是多条消息。
所以你可能会说,只是保持消息简短,但我希望能够同时做到这两个 - 为所有应用程序创建一个精美的消息(可能添加嵌入图像和链接),但如果它是用于 SMS,则使其简短。
我所做的是创建一个包含要使用的意图的对话框,而对于短信我只是将消息设置为更短。
问题
我该怎么做?
我尝试了下一个代码,但由于某种原因它没有显示发送短信的电话号码。
这是代码:
创建并显示对话框:
final String phoneNumber = "12346556", email = "test@gmail.com";
final String smsMessage="sms", message="message", title="title";
final PackageManager pm = getPackageManager();
//
final Uri uri = Uri.parse("smsto:" + phoneNumber);
final Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("sms_body", smsMessage);
smsIntent.putExtra("address", new String[] { phoneNumber });
final List<ResolveInfo> smsResInfo = pm.queryIntentActivities(smsIntent, 0);
final Set<String> smsPackages = new HashSet<String>();
for (final ResolveInfo ri : smsResInfo)
smsPackages.add(ri.activityInfo.packageName + ri.activityInfo.name);
//
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
shareIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, new String[] { phoneNumber });
final List<ResolveInfo> shareResInfo = pm.queryIntentActivities(shareIntent, 0);
//
final List<ResolveInfo> mergedResInfo = new ArrayList<ResolveInfo>(smsResInfo);
for (final ResolveInfo resolveInfo : shareResInfo) {
if (smsPackages.contains(resolveInfo.activityInfo.packageName + resolveInfo.activityInfo.name))
continue;
mergedResInfo.add(resolveInfo);
}
//
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final String[] items = new String[mergedResInfo.size()];
for (int i = 0; i < mergedResInfo.size(); i++) {
final ResolveInfo resolveInfo = mergedResInfo.get(i);
items[i] = resolveInfo.loadLabel(pm).toString();
}
//
final ListAdapter adapter = new ArrayAdapterWithIcon(MainActivity.this, items, mergedResInfo);
builder.setTitle("Select app").setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int item) {
final ResolveInfo resolveInfo = mergedResInfo.get(item);
Intent intent;
if (smsPackages.contains(resolveInfo.activityInfo.packageName + resolveInfo.activityInfo.name))
intent = smsIntent;
else
intent = shareIntent;
intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);
}
}).show();
ArrayAdapterWithIcon 类:
public class ArrayAdapterWithIcon extends ArrayAdapter<String> {
private final List<ResolveInfo> mMergedResInfo;
public ArrayAdapterWithIcon(final MainActivity context, final String[] items,
final List<ResolveInfo> mergedResInfo) {
super(context, android.R.layout.select_dialog_item, items);
this.mMergedResInfo = mergedResInfo;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView textView = (TextView) view.findViewById(android.R.id.text1);
final Drawable icon = mMergedResInfo.get(position).loadIcon(getPackageManager());
textView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
textView.setCompoundDrawablePadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,
getContext().getResources().getDisplayMetrics()));
return view;
}
}
我该怎么做才能修复此代码?