android developer
android developer

Reputation: 115952

create a share intent with respect to sms messages

background

it's very common to share content by using intents:

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

it's even possible to add receipients to the same sharing intent :

shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
shareIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, new String[] { phoneNumber });

problem

the problem is the content of the message itself. on SMS, messages can cost money, so it's important to keep them short in order to send only a single message instead of multiple messages.

so you might say , just keep the message short, but i would like to be able to do both - create a fancy message for all apps (maybe add embed images and links), except that if it's for SMS, make it short.

what i've done is to create a dialog with the intents to be used, while for sms i simply set the message to be shorter.

question

how can i do it?

i've tried the next code , but it for some reason it doesn't show the phone number for sms sending.

here's the code:

create and show the dialog:

final String phoneNumber = "12346556", email = "[email protected]";
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 class:

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

}

what can i do in order to fix this code?

Upvotes: 1

Views: 2267

Answers (1)

android developer
android developer

Reputation: 115952

ok ,the problem is with those lines:

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

should be:

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

if you need to send to more numbers, just add "," between them.

i also think the part with the EXTRA_PHONE_NUMBER isn't needed.

Upvotes: 1

Related Questions