ACTION_SEND用于发送短信

时间:2011-04-26 09:05:33

标签: android android-activity sms

我想打开本机应用程序发送短信但应该已经有电话号码了。我找到了ACTION_SEND但是当我调用我的函数时,返回错误:

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

我的代码在这里提供:

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

我知道这很简单,但我不知道为什么它不起作用,我找不到任何有用的信息。

感谢您的任何建议。

5 个答案:

答案 0 :(得分:46)

为什么,这应该可以正常工作。 http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO

查看我的代码:

Uri uri = Uri.parse("smsto:0800000123");   
Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
it.putExtra("sms_body", "The SMS text");   
startActivity(it); 

答案 1 :(得分:1)

我认为您应该使用以下代码:

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

我认为这可能对你有帮助。

答案 2 :(得分:1)

感谢您的信息! 以下是使用之前信息的解决方案:

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

最好的问候。

答案 3 :(得分:0)

在我这边,没有uri参数的意图适用于所有设备, 我需要使用Pixel Phone的地方除外,所以我检查了两种方式:

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

答案 4 :(得分:0)

在kotlin中,以下代码适用于号码和自定义消息

Matcher(nlp.vocab, validate=True)