在我的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();
}
}
}
帮我处理任何代码!
Thanx in Adavance !!
答案 0 :(得分:7)
试试这个链接,
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" />
答案 1 :(得分:1)
发送短信使用此方法
public void sendTextMessage (String destinationAddress, String scAddress, String text,
PendingIntent sentIntent, PendingIntent deliveryIntent)
例如
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
其中deliverPI为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;
}
}
}