在使用广播接收器检查短信状态时,它在发送短信时显示吐司,但在未发送或发送短信时不显示任何内容(我通过输入一个突然的数字来测试它)。我使用的代码是我在每个检查短信发送状态的网站上看到最多的代码。但是我的代码仅在成功发送短信时显示状态。任何人都可以暗示我做错了什么吗?我在 doInBackground() 中有这个方法,所以很明显我正在使用 AsyncTask。
public void send_SMS(String list, String msg, AtaskClass task)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(context, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(context, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
StringTokenizer st = new StringTokenizer(list,",");
int count= st.countTokens();
int i =1;
count = 1;
while(st.hasMoreElements())
{
// PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this, SMS.class),0);
String tempMobileNumber = (String)st.nextElement();
//SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(tempMobileNumber, null, msg , sentPI, deliveredPI);
Double cCom = ((double)i/count) * 100;
int j = cCom.intValue();
task.doProgress(j);
i++;
count ++;
}
// class ends
}