我有以下代码,我想知道哪些短信已经发送。 我的代码每30秒发送1-5个短信,所以当吐司“短信未送达”出现时,我不知道哪一个没有发送。我不知道这是否是正确的方法,但这是解决这个问题的最常见方法
public boolean sendSMS(String id, String num, 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(sendBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliveryBroadcastReciever, new
IntentFilter(DELIVERED));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("xxxxxxxx", null, msg, sentPI, deliveredPI);
Log.e("Message Sent", num + " " + msg + " " + id);
return true;
}
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;
}
}
}
class SentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, 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;
}
}
我很抱歉我的英语不好。
编辑:
public boolean sendSMS(String id, String num, 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(sendBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
smsManager.sendTextMessage("+xxxxxxxxxx", null, msg, sentPI, deliveredPI);
Log.e("Message Sent", num + " " + msg + " " + id);
return true;
}
public class DeliverReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null){
final Object[] bObject = (Object[]) bundle.get("pdus");
for (int i = 0; i < bObject.length; i++){
SmsMessage current = SmsMessage.createFromPdu((byte[]) bObject[i]);
String phoneNum = current.getDisplayOriginatingAddress();
}
}
}catch (Exception e){
Log.e("Deliver Reciever",e.toString());
}
}
}
调试屏幕截图:http://imgur.com/a/GulgP
答案 0 :(得分:0)
首先,使您的SmSManager成为静态类对象。然后,您可以从意图对象中隐藏的PDU中获取当前的电话号码/电话号码。
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
String phone = "";
SmsMessage smsMessage;
try {
if (bundle != null) {
final Object[] pdu = (Object[]) bundle.get("pdu");
for (int i = 0; i < pdu.length; i++) {
smsMessage = SmsMessage.createFromPdu((byte[]) pdu[i]);
phone = current.getDisplayOriginatingAddress();
}
}
}
}