我有一种发送短信的方法。此方法注册两个待处理的意图以接收发送和发送报告。
我发送第一条消息,然后收到其发送和发送报告,然后发送另一条消息。在接收第二条消息的报告时,我收到第一条消息和第二条消息的报告。如果我发送另一条消息,我会收到第一条和第二条消息的报告,然后是第三条消息。
有什么问题?
来源:
public void sendSms(String phoneNumber, String cmd ...) {
sentPI = PendingIntent.getBroadcast(this, sendSmsCounter++, new Intent(SENT), PendingIntent.FLAG_CANCEL_CURRENT);
deliveredPI = PendingIntent.getBroadcast(this, deliverCounter++, new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);
sendingBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
displayMessage("Sms Sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
displayMessage("Sending SMS error: Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
displayMessage("Sending SMS error: No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
displayMessage("Sending SMS error: Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
displayMessage("Sending SMS error: Radio off");
break;
}
}
};
this.registerReceiver(sendingBroadcastReceiver, new IntentFilter(SENT));
deliveryBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
displayMessage("SMS Delivered");
break;
case Activity.RESULT_CANCELED:
displayMessage("SMS delivery error: SMS Canceled");
break;
}
}
};
this.registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, cmd, sentPI, deliveredPI);
答案 0 :(得分:2)
使用此代码发送和接收已发送的通知
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneNo = "Phone number";
String message = "Content of the msg";
if (phoneNo.length() > 0 && message.length() > 0) {
TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
displayAlert();
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
// do something
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
// do something
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
// do something
break;
case TelephonyManager.SIM_STATE_READY:
// do something
sendSMS(phoneNo, message);
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
// do something
break;
}
} else {
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
private void sendSMS(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(YourActivity.this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(YourActivity.this,
0, new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(YourActivity.this, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(YourActivity.this, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(YourActivity.this, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(YourActivity.this, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(YourActivity.this, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(YourActivity.this, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
Intent smsintent = new Intent(YourActivity.this, SecondActivity.Activity);
startActivity(smsintent);
}
答案 1 :(得分:2)
问题:
MyActivity.this.unregisterReceiver(this);
仔细看看:
sendingBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
displayMessage("Sms Sent");
break;
// ...
}
MyActivity.this.unregisterReceiver(this);
}
};
在接收广播和处理后,我们不再需要这个广播接收器,所以我们从我们的活动中取消注册。
答案 2 :(得分:0)
你把它放在你的onPause吗?
@Override
protected void onPause() {
super.onPause();
if (bRSMS_has_been_sent != null){
unregisterReceiver(bRSMS_has_been_sent);
bRSMS_has_been_sent = null;
}
if (brSMS_has_beed_delivered != null){
unregisterReceiver(brSMS_has_beed_delivered);
brSMS_has_beed_delivered = null;
}
}
其中bRSMS_has_been_sent
和brSMS_has_beed_delivered
是我的收件人
确保取消注册接收器,否则你会有几个闲置。
如果您想看到自己,只需使用静态变量并在每次调用发送方法时增加它,并在接收器中执行日志输出。这样你就可以看到哪个接收器实际被触发了。如果您注册所有接收器并且从不取消注册任何接收器,您应该看到几个接收器被触发。