我在点击发送按钮时调用此方法我收到此错误
java.lang.IllegalArgumentException:Receiver未注册:
并在unregisterReceiver(new BroadcastReceiver() {}
private void sendSMS(String edittextSmsNumber, String msg) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
/* base64msgString = Base64.encodeToString(
encryptedText, 0);*/
if (senderinfo != null && timeStamp != 0
&& msgString != null) {
System.out.println("entered into database");
outboxDatabase.open();
outboxDatabase.insertintoOutbox(base64msgString, key,
savedPhoneno, timeStamp);
outboxDatabase.close();
}
Intent homeScreenIntent = new Intent();
homeScreenIntent.setClass(getApplicationContext(),
HomeScreen.class);
startActivity(homeScreenIntent);
finish();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "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));
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
// short port = 8888;
sms.sendTextMessage(edittextSmsNumber, null, msg,
sentPI, deliveredPI);
try {
unregisterReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
context.unregisterReceiver(this);
}
});
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
错误在于说发生了什么 - 您正在尝试取消注册尚未注册的新接收器。
您应该将接收器定义为您的类的成员,替换您的动态实例化。然后在每个成员接收器上调用unregisterReceiver
。
我认为它会解决它。