Android SMS Delivery Status

老子叫甜甜 提交于 2019-12-23 18:10:27

问题


I need to implement a function in my android app to send Sms messages. I found many tutorials regarding this but can NOT get the delivered status (Fail or OK). Following is my sms method.

 private void sendSmsMessageWithStatus(String phoneNumber, String Msg)
 {
     String SENT = "SMS_SENT";
     String DELIVERED = "SMS_DELIVERED";

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

     registerReceiver(new BroadcastReceiver()
     {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            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));

     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 sms = SmsManager.getDefault();
     sms.sendTextMessage(phoneNumber, null, Msg, sentPI, deliveredPI);     
 }

I am checking with my google nexus 4 phone and gets the sms messages delivered properly.

I am getting the message "SMS sent" message but never gets "SMS delivered" message even when I sent successful messages to a different number.

Also the same thing when I check with the emulator (Display "SMS sent" message but NOT delivery status message "SMS not delivered").

In the manifest file I have added the permission

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

Also my development SDK versions as follows.

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

Does nayone know what I am doing wrong for NOT to get the Delivery status. Accoring to my code "SMS delivered" OR "SMS not delivered" messages. Thanks in advance!!!

EDITS

Also the sms send permission have added to manifest. missed mention that in question...

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

回答1:


Add this permission to your manifest:

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


来源:https://stackoverflow.com/questions/30327042/android-sms-delivery-status

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!