ACTION_SEND used to send sms

2019-02-13 13:01发布

I want to open native application to send sms but there should be already phone number. I found ACTION_SEND but when I'm calling my function it's return error that:

04-26 11:59:15.991: ERROR/AndroidRuntime(20198): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO (has extras) }

My code presented here:

    private void smsSend(String number) {
    Intent intent = new Intent(Intent.ACTION_SENDTO, null);
    intent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
    startActivity(intent);
}

I know that's is simple but I don't know why it does not work and I can not find any helfull information.

Thanks for any advice.

4条回答
Luminary・发光体
2楼-- · 2019-02-13 13:14

On my side, the intent without uri parameter work for all devices, except for Pixel Phone where I need to use it, so I check the 2 ways:

    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    final Context context = activity.getApplicationContext();
    final String phoneNumber = "1234567890";
    final String msg = "Hello!";
    smsIntent.putExtra("address", phoneNumber);
    smsIntent.putExtra("sms_body", msg);
    smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
            Intent.FLAG_ACTIVITY_CLEAR_TOP);

    final PackageManager manager = context.getPackageManager();
    List<ResolveInfo> infos = manager.queryIntentActivities(smsIntent, 0);
    if (infos.size() <1) {
        //No Application can handle your intent
        //try in a another way ...
        Uri uri = Uri.parse("smsto:"+phoneNumber);
        smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
        smsIntent.putExtra("sms_body", msg);
        infos = manager.queryIntentActivities(smsIntent, 0);
    }

    if (infos.size() <1) {
        //No Application can handle your intent
        Log.e("SendMessage","No Application can handle your SMS intent");
    }
查看更多
狗以群分
3楼-- · 2019-02-13 13:18

Why, this should work fine. http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO

Check out my code:

Uri uri = Uri.parse("smsto:0800000123");   
Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
it.putExtra("sms_body", "The SMS text");   
startActivity(it); 
查看更多
别忘想泡老子
4楼-- · 2019-02-13 13:27

I think you should use the following code:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);

//use to fill the sms body
StringBuilder uri = new StringBuilder("sms:" + mobilenumber);
sendIntent.putExtra("sms_body", "");
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.setData("");
startActivity(sendIntent);

I think this may help you.

查看更多
不美不萌又怎样
5楼-- · 2019-02-13 13:28

Thanks for the info ! Here is my solution using the previous info:

if (url.indexOf("tel:") > -1) {
    startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
    return true;
}
else if (url.indexOf("sms:") > -1){
    startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
    return true;
}

Best regards.

查看更多
登录 后发表回答