使用intent android发送短信时出错

时间:2015-08-26 11:22:20

标签: android

我正在尝试发送短信,并希望获得发送报告。 我正在使用此代码: 我还在清单

中添加了短信发送权限
     public void sendSMS(String phoneNumber,String message) {
    SmsManager smsManager = SmsManager.getDefault();


    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(message);
    int messageCount = parts.size();

    Log.i("Message Count", "Message Count: " + messageCount);

    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    for (int j = 0; j < messageCount; j++) {
        sentIntents.add(sentPI);
        deliveryIntents.add(deliveredPI);
    }

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    // ---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {

                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));

    smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
       /* sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); */
}

我收到此错误:

     30533-30533/com.digicare.drnadeemaslam E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.SecurityException: Sending SMS message: Neither user 10082 nor current process has android.permission.SEND_SMS.
        at android.os.Parcel.readException(Parcel.java:1327)
        at android.os.Parcel.readException(Parcel.java:1281)
        at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:609)
        at android.telephony.gemini.GeminiSmsManager.sendTextMessageGemini(GeminiSmsManager.java:139)
        at android.telephony.SmsManager.sendTextMessage(SmsManager.java:143)
        at android.telephony.gsm.SmsManager.sendTextMessage(SmsManager.java:79)
        at com.digicare.drnadeemaslam.MainActivity.sendSMS(MainActivity.java:112)
        at com.digicare.drnadeemaslam.MainActivity.onClick(MainActivity.java:36)
        at android.view.View.performClick(View.java:3517)
        at android.view.View$PerformClick.run(View.java:14155)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4511)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
        at dalvik.system.NativeStart.main(Native Method)

我需要发送消息并希望获得发送报告,天气信息是否发送。

1 个答案:

答案 0 :(得分:2)

在Manifest.xml中的应用程序标记上方添加此权限。

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