简体   繁体   中英

Open MMS messaging default screen with attachment using intent in android

My requirement is that I have to open directly (without app chooser) messaging default android screen from my application with to,body and vcf attachment. I am using below two methods(Approach). But in first approach, attachment is coming but multiple app chooser screen comes first then I have to choose Messaging app.

In second approach, default messaging app is opening but attachment (.vcf) file is not coming. Please advice. Below is the code.

Approach 1:

public static void sendMMS(Context ctx,String firstname,String send_to,String body,String vcard)
    {

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/x-vcard");
        sendIntent.putExtra("address", send_to);
        sendIntent.putExtra("sms_body", body);

      File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),firstname+".vcf");
        sendIntent.putExtra(Intent.EXTRA_STREAM,
                Uri.fromFile(file1));
        ((Activity) ctx).startActivity(sendIntent);
    }

Approach 2:

private void sendMMS()
{
    Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
    smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
    smsIntent.setType("vnd.android-dir/mms-sms");
     File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"file.vcf");
     smsIntent.putExtra(Intent.EXTRA_STREAM,
                Uri.fromFile(file1));
    smsIntent.setData(Uri.parse("sms:" + "XXXXXXXXXXX")); 
    startActivity(smsIntent);
}

I got the solution finally as below. The main issue was that 1. Add ClassName in Intent and 2. Use file path starting from file:// and put it into Uri.parse()

Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
        sendIntent.putExtra("sms_body", body); 
        sendIntent.putExtra("address", send_to);
        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Test.vcf"));
        sendIntent.setType("text/x-vcard");
        startActivity(sendIntent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM