创建关于短信消息的共享意图

时间:2013-08-19 13:51:19

标签: android android-intent sms share

背景

通过使用意图分享内容非常常见:

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,使它短。

我所做的是创建一个包含要使用的意图的对话框,而对于短信,我只是将消息设置得更短。

问题

我该怎么做?

我已经尝试了下一个代码,但由于某些原因它没有显示发送短信的电话号码。

这是代码:

创建并显示对话框:

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;
    }

}

我该怎么做才能修复此代码?

1 个答案:

答案 0 :(得分:1)

好的,问题出在这些方面:

final Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("address", new String[] { phoneNumber });

应该是:

final Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("address", phoneNumber);

如果您需要发送更多号码,只需在它们之间添加“,”。

我也认为不需要EXTRA_PHONE_NUMBER的部分。