我正在开发一个android sms应用程序。我用以下代码发送短信。
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()
之前未注册。所以onReceive()开关块()在大多数情况下都无法访问。如果我在发送第二个短信时没有写取注册呼叫。onreceive()
将听第一个和第二个短信。
因此,请在完成onreceive()
之后通过取消注册来帮助解决此问题。
答案 0 :(得分:0)
我部分地解决了这个问题。 我从sms方法的末尾删除了取消注册(广播)并将其放在onreceive()方法中。因此,只有在到达onreceive()之后才会发生unregister()。我的工作代码粘贴在下面。
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;
}
context.unregister(this);
}
};
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;
}
context.unregister(this)
}
};
registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
现在我的代码适用于sendBroadcastReceiver。
但是在deliveryBroadcastReciever的情况下,接收器()永远不会听任何传递。所以deliveryBroadcastReciever正在注册每个发送短信。
所以我尝试使用onstop()
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
unregister(deliveryBroadcastReceiver);
}
但onStop()仅取消注册最后一次注册的deliveryBroadcastReceiver对象。如果我发送两次sms,将会有两个deliveryBroadcastReceiver对象,而onstop()将仅取消注册最后一个对象。请帮帮我朋友。