我有一个活动(称为 NMessageActivity ),其中包含发送短信的简单方法。只需使用此代码:
public void sendSMS(MBean dBean) {
if (sentSMS != null && deliveredSMS != null) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(dBean.getToInNumbers(), null,
dBean.getMessage(), sentSMS, deliveredSMS);
} else {
ToastObject.postMessage("Error code 233!");
}
}
在此之前,我确实初始化了我使用的 Pending Intents 。我把它们放在 onCreate方法我有:
smsSentReceiver = new SentReceiver(this);
smsDeliveredReceiver = new DeliveredReceiver(this);
然后我在 onResume():
注册它们super.onResume();
registerReceiver(smsSentReceiver, new IntentFilter(TAG_SENT));
registerReceiver(smsDeliveredReceiver, new IntentFilter(TAG_DELIVERED));
并在 onPause()我取消注册:
super.onPause();
unregisterReceiver(smsSentReceiver);
unregisterReceiver(smsDeliveredReceiver);
在发送短信之前,我准备 AlarmManager 来执行意图 2秒后使用此代码:
MBean dbbean = new MBean(txt_destNum.getText().toString(), txt_date
.getText().toString(), txt_time.getText().toString(),
MBean.STATUS_WAIT, txt_msg.getText().toString());
db.addMBean(dbbean);
Intent intentAlarm = new Intent(TAG_SENT);
Intent intentAlarm2 = new Intent(TAG_DELIVERED);
// putting the values in
intentAlarm.putExtra(MBean.MBEAN_NAME, dbbean.toString());
sentSMS = PendingIntent.getBroadcast(getApplicationContext(), 0,
intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
deliveredSMS = PendingIntent.getBroadcast(getApplicationContext(),
0, intentAlarm2, PendingIntent.FLAG_UPDATE_CURRENT);
int sec = 2;
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (sec * 1000), sentSMS);
问题不在于上述方法,也不在于AlarmManager。 相反,问题出在我拥有的BroadcastReceiver上。 从不调用广播接收器。为什么会这样?
以下是Broadcast Receiver类的详细信息:
SentReceiver.java
public class SentReceiver extends BroadcastReceiver {
private MBean dBean;
private Context dCont;
private ToastSender dToast;
private Activity dNMessageActivity;
public SentReceiver(Activity obAct){
dNMessageActivity = obAct;
}
private void sendMessage() {
((NMessageActivity)dNMessageActivity).sendSMS(dBean);
}
private void vibratePhone() {
Vibrator vibrator = (Vibrator) dCont
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
@Override
public void onReceive(Context arg0, Intent arg1) {
dToast = new ToastSender(arg0);
dCont = arg0;
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
// SMS has been sent
dBean = new MBean(arg1.getStringExtra(MBean.MBEAN_NAME));
vibratePhone();
sendMessage();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
// Generic Failure
dToast.postMessage("Error code 48!");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
// No Service
dToast.postMessage("Error code 53!");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
// Null PDU
dToast.postMessage("Error code 58!");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
// Radio Off
dToast.postMessage("Error code 63!");
break;
default:
break;
}
}
}
和另一个接收者是
DeliveredReceiver.java
public class DeliveredReceiver extends BroadcastReceiver {
private ToastSender dToast;
private Context dCont;
private Activity dNMessageActivity;
public DeliveredReceiver(Activity obAct){
dNMessageActivity = obAct;
}
private void vibratePhone() {
Vibrator vibrator = (Vibrator) dCont
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
@Override
public void onReceive(Context arg0, Intent arg1) {
dCont = arg0;
dToast = new ToastSender(arg0);
switch (getResultCode()) {
case Activity.RESULT_OK:
dToast.postMessage("SMS Sent");
vibratePhone();
break;
case Activity.RESULT_CANCELED:
dToast.postMessage("SMS Pending");
break;
}
}
}
我想知道广播接收器问题或我通过Alarmmanager传递的Pending Intent问题?为什么程序根本不调用广播接收器?