Example usage for android.content Intent ACTION_SENDTO

List of usage examples for android.content Intent ACTION_SENDTO

Introduction

In this page you can find the example usage for android.content Intent ACTION_SENDTO.

Prototype

String ACTION_SENDTO

To view the source code for android.content Intent ACTION_SENDTO.

Click Source Link

Document

Activity Action: Send a message to someone specified by the data.

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc//w  w  w  .  j av a  2  s.c  o m
 */
public void sendMessage(String[] recipients, String subject, Message msg) {
    if (editInProgress()) {
        stopEditing(true);
    }
    Intent emailIntent;
    String attachment = msg.getAttachment();
    boolean hasAttachment = (attachment != null && attachment.length() > 0) || msg.getAttachments().size() > 0;

    if (msg.getMimeType().equals(Message.MIME_TEXT) && !hasAttachment) {
        StringBuilder to = new StringBuilder();
        for (int i = 0; i < recipients.length; i++) {
            to.append(recipients[i]);
            to.append(";");
        }
        emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + to.toString() + "?subject="
                + Uri.encode(subject) + "&body=" + Uri.encode(msg.getContent())));
    } else {
        if (hasAttachment) {
            if (msg.getAttachments().size() > 1) {
                emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
                emailIntent.setType(msg.getMimeType());
                ArrayList<Uri> uris = new ArrayList<Uri>();

                for (String path : msg.getAttachments().keySet()) {
                    uris.add(Uri.parse(fixAttachmentPath(path)));
                }

                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            } else {
                emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
                emailIntent.setType(msg.getMimeType());
                emailIntent.setType(msg.getAttachmentMimeType());
                //if the attachment is in the uder home dir we need to copy it
                //to an accessible dir
                attachment = fixAttachmentPath(attachment);
                emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachment));
            }
        } else {
            emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
            emailIntent.setType(msg.getMimeType());
        }
        if (msg.getMimeType().equals(Message.MIME_HTML)) {
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(msg.getContent()));
        } else {
            /*
            // Attempted this workaround to fix the ClassCastException that occurs on android when
            // there are multiple attachments.  Unfortunately, this fixes the stack trace, but
            // has the unwanted side-effect of producing a blank message body.
            // Same workaround for HTML mimetype also fails the same way.
            // Conclusion, Just live with the stack trace.  It doesn't seem to affect the
            // execution of the program... treat it as a warning.
            // See https://github.com/codenameone/CodenameOne/issues/1782
            if (msg.getAttachments().size() > 1) {
            ArrayList<String> contentArr = new ArrayList<String>();
            contentArr.add(msg.getContent());
            emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, contentArr);
            } else {
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg.getContent());
                    
            }*/
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg.getContent());
        }

    }
    final String attach = attachment;
    AndroidNativeUtil.startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."),
            new IntentResultListener() {

                @Override
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    if (attach != null && attach.length() > 0 && attach.contains("tmp")) {
                        FileSystemStorage.getInstance().delete(attach);
                    }
                }
            });
}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc/*from  w ww .  ja  va 2  s.co m*/
 */
public void sendSMS(final String phoneNumber, final String message, boolean i) throws IOException {
    if (!checkForPermission(Manifest.permission.SEND_SMS, "This is required to send a SMS")) {
        return;
    }
    if (!checkForPermission(Manifest.permission.READ_PHONE_STATE, "This is required to send a SMS")) {
        return;
    }
    if (i) {
        Intent smsIntent = null;
        if (android.os.Build.VERSION.SDK_INT < 19) {
            smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", phoneNumber);
            smsIntent.putExtra("sms_body", message);
        } else {
            smsIntent = new Intent(Intent.ACTION_SENDTO);
            smsIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
            smsIntent.putExtra("sms_body", message);
        }
        getContext().startActivity(smsIntent);

    } else {
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);
        sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
    }
}

From source file:com.zoffcc.applications.zanavi.Navit.java

void sendEmailWithAttachment(Context c, final String recipient, final String subject, final String message,
        final String full_file_name, final String full_file_name_suppl) {
    try {/*from  w ww.  j  a  va 2s .  c om*/
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", recipient, null));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        ArrayList<Uri> uris = new ArrayList<>();
        uris.add(Uri.parse("file://" + full_file_name));
        try {
            if (new File(full_file_name_suppl).length() > 0) {
                uris.add(Uri.parse("file://" + full_file_name_suppl));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(emailIntent, 0);
        List<LabeledIntent> intents = new ArrayList<>();

        if (resolveInfos.size() != 0) {
            for (ResolveInfo info : resolveInfos) {
                Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                System.out.println(
                        "email:" + "comp=" + info.activityInfo.packageName + " " + info.activityInfo.name);
                intent.setComponent(new ComponentName(info.activityInfo.packageName, info.activityInfo.name));
                intent.putExtra(Intent.EXTRA_EMAIL, new String[] { recipient });
                if (subject != null)
                    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                if (message != null)
                    intent.putExtra(Intent.EXTRA_TEXT, message);
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                intents.add(new LabeledIntent(intent, info.activityInfo.packageName,
                        info.loadLabel(getPackageManager()), info.icon));
            }
            Intent chooser = Intent.createChooser(intents.remove(intents.size() - 1),
                    Navit.get_text("Send email with attachments"));
            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new LabeledIntent[intents.size()]));
            startActivity(chooser);
        } else {
            System.out.println("email:" + "No Email App found");
            new AlertDialog.Builder(c).setMessage(Navit.get_text("No Email App found"))
                    .setPositiveButton(Navit.get_text("Ok"), null).show();
        }

        //         final Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", recipient, null));
        //         if (recipient != null) emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient });
        //         if (subject != null) emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        //         if (message != null) emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
        //         if (full_file_name != null)
        //         {
        //            emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + full_file_name));
        //            //ArrayList<Uri> uris = new ArrayList<>();
        //            //uris.add(Uri.parse("file://" + full_file_name));
        //            //emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //ArrayList<Uri> of attachment Uri's
        //         }
        //
        //         List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(emailIntent, 0);
        //         if (resolveInfos.size() != 0)
        //         {
        //            String packageName = resolveInfos.get(0).activityInfo.packageName;
        //            String name = resolveInfos.get(0).activityInfo.name;
        //
        //            emailIntent.setAction(Intent.ACTION_SEND);
        //            emailIntent.setComponent(new ComponentName(packageName, name));
        //
        //            System.out.println("email:" + "comp=" + packageName + " " + name);
        //
        //            startActivity(emailIntent);
        //         }
        //         else
        //         {
        //            System.out.println("email:" + "No Email App found");
        //            new AlertDialog.Builder(c).setMessage(Navit.get_text("No Email App found")).setPositiveButton(Navit.get_text("Ok"), null).show();
        //         }

    } catch (ActivityNotFoundException e) {
        // cannot send email for some reason
    }
}