如何在 Android 中发送短信?

How do I send a text message in Android?

如何发送短信?这看起来很简单,但对我不起作用。

我在清单中有权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.healthapp.healthapp">

<uses-permission android:name="android.permission.SEND_SMS"/>

<application   ...

然后我的 onClick 中有这段代码:

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
                            startActivity(sendIntent);

但是当我 运行 这个我得到 "Unfortunately App has stopped."

以及错误信息:

FATAL EXCEPTION: main
Process: com.healthapp.healthapp, PID: 13477
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }

您的代码中有两种不同的发送 SMS 的方法。如果您想使用 SmsManager,则不需要 Intent/startActivity() 方法,该方法会尝试打开另一个应用程序来处理短信。

您可以删除 smsManager.sendTextMessage() 行之后的所有内容,您将不会再得到 Exception

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);

如果你想通过 Intent 那么

sendSmsByViewIntent()

Intent smsVIntent = new Intent(Intent.ACTION_VIEW);
    // prompts only sms-mms clients
    smsVIntent.setType("vnd.android-dir/mms-sms");

    // extra fields for number and message respectively
    smsVIntent.putExtra("address", phoneNumber.getText().toString());
    smsVIntent.putExtra("sms_body", smsBody.getText().toString());
    try{
        startActivity(smsVIntent);
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

然后通过SmsManager发送,

sendSmsByManager()

try {
        // Get the default instance of the SmsManager
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber.getText().toString(), 
                null,  
                smsBody.getText().toString(), 
                null, 
                null);
        Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),"Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

由 ,

发送

ACTION_SENDTO

// add the phone number in the data
    Uri uri = Uri.parse("smsto:" + phoneNumber.getText().toString());

    Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
    // add the message at the sms_body extra field
    smsSIntent.putExtra("sms_body", smsBody.getText().toString());
    try{
        startActivity(smsSIntent);
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

应该尝试这些方法中的任何一种。但是您正在同时尝试两种类型。

最后最主要的是 manifest.xml

的许可
<uses-permission android:name="android.permission.SEND_SMS"/>