我正在开发一个 android 短信应用程序。我用来发送短信的以下代码。
public void sendsms(){
sendBroadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
System.out.println("hereeeeeeeeeeeeeeeeeeeeeeeeeeeeee8888888888888888");
System.out.println("1111111111111111111111111111111111");
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
afterSend();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
System.out.println("222222222222222222222222222");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
System.out.println("333333333333333333333333333333333");
ContentValues values1 = new ContentValues();
values1.put("address", phoneNumber);
values1.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/queued"), values1);
afterSend();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
System.out.println("44444444444444444444444444");
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
System.out.println("4444444444444444444444444444444444");
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));
//--- when the SMS hasbeen delivered---
deliveryBroadcastReciever=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode())
{
case Activity.RESULT_OK:
System.out.println("okkkkkkkkkkkkkkkkkkkkkkkkkkk");
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
System.out.println("cancellllllllllllllllllll");
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReciever);
}
现在我的问题是,如果我发送一条短信,在sentsms
到达onreceive()
.So onReceive() 开关 block() 之前未注册的广播在大多数情况下是无法访问的。如果我不写 unregister 调用,那么在发送第二个 sms 时onreceive()
。将同时监听第一个和第二个 sms 。
因此,请在完成后通过取消注册呼叫来帮助解决此问题onreceive()
。