android 如何获取已发送短信的送达报告?

xwmevbvl  于 2022-11-27  发布在  Android
关注(0)|答案(2)|浏览(258)

在我的Application中,我使用SMS Manager发送短信。为了检查消息是否发送,我使用Toast。而不是Toast,我想得到SMS Delivery Report。我尝试了许多例子,但有一个流程不显示交付报告,如如果我的余额为0时,我的代码发送短信,我想找到短信发送失败交付报告时,我的余额为0。
编码:

String phoneNo = phno.getText().toString();
String smsBody = body.getText().toString(); 
 //Selected Messages to report to 1909
{
             try
             {
                
                 SmsManager smsManager = SmsManager.getDefault();
                 smsManager.sendTextMessage(phoneNo, null, smsBody, null, null); 
                 Toast.makeText(getApplicationContext(), "SMS Sent to: "+phoneNo,Toast.LENGTH_SHORT).show();
             }                           
             catch (Exception e)
             {
                 //Error Occurred if No Messages Selected 
                 Toast.makeText(getApplicationContext(),"SMS failed, please try again later!",Toast.LENGTH_SHORT).show();
                 e.printStackTrace();
             }
             }
         
    }
cbeh67ev

cbeh67ev1#

试试这个链接,
SMS Delivery Report in Android

class deliverReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, 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;
            }

        }
    }

别忘了在清单中添加这些权限

<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission android:name="android.permission.SEND_SMS" /> 
<uses-permission android:name="android.permission.READ_SMS" /> 
<uses-permission android:name="android.permission.WRITE_SMS" />
zvokhttg

zvokhttg2#

使用此方法发送SMS

public void sendTextMessage (String destinationAddress, String scAddress, String text, 
PendingIntent sentIntent, PendingIntent deliveryIntent)

例如

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

其中deliveredPI是PendingIntent
最后一个参数是deliveryIntent,它由Android在“消息成功传递”到接收者时触发。
所以现在你需要有一个BroadcastReceiver来获得这个消息。

BroadcastReceiver deliveryBroadcastReciever = new deliverReceiver();

class deliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, 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;
        }

    }
}

相关问题