使用broadcastreciver检查sms状态时,它会在发送sms时显示toast,但在没有发送或传送sms时显示任何内容(我通过输入一个突然的数字来测试它)。 使用的代码是我在每个检查短信发送状态的网站上看到的最多的代码。但我的代码只显示短信成功发送时的状态。 任何人都能明白我做错了什么吗? 我在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
}