我用下面的代码发送一个文本(pre-kit-kat)。当我创建要传递给smsmanager的意图时,我在意图中添加了一个int。在广播接收器中,当我调用intent.getintextra时,它一直给我一个0的值。你知道为什么吗?
当我将onreceive中的i值打印到日志中时,它每次都显示值为0
这是发送代码(这是for循环,所以“i”可以是0到10之间的任何值):
for (int i = 0; i < getSelectedNumbers().size(); i++) {
Log.d("Pete Debug", "Loop value of i = " + String.valueOf(i));
String number = removeSpecialCharacters(getSelectedNumbers().get(i));
String smsSending = "Sending to " + number + "... ";
Log.d("Pete Debug", smsSending);
statusItems = new SmsStatusItems();
statusItems.setMessage(smsSending);
statusItems.setImage(R.drawable.clock);
smsStatusAdapter.add(statusItems);
Intent sentIntent = new Intent(SMS_SENT);
sentIntent.putExtra(SMS_SENT, i);
Intent deliveredIntent = new Intent(SMS_DELIVERED);
deliveredIntent.putExtra(SMS_DELIVERED, i);
mSmsManager.sendTextMessage(number, null, getFinalMessage(),
(PendingIntent.getBroadcast(getActivity(), 0, sentIntent, 0)),
(PendingIntent.getBroadcast(getActivity(), 0, deliveredIntent, 0)));
}
这是广播接收器:
smsSentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int i = intent.getIntExtra(SMS_SENT, -1);
Log.d("Pete Debug", "Sent value of i = " + i);
switch (getResultCode()) {
case Activity.RESULT_OK:
smsStatusAdapter.getItem(i).setImage(R.drawable.pass);
smsStatusAdapter.notifyDataSetChanged();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
smsStatusAdapter.getItem(i).setImage(R.drawable.fail);
smsStatusAdapter.notifyDataSetChanged();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
smsStatusAdapter.getItem(i).setImage(R.drawable.fail);
smsStatusAdapter.notifyDataSetChanged();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
smsStatusAdapter.getItem(i).setImage(R.drawable.fail);
smsStatusAdapter.notifyDataSetChanged();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
smsStatusAdapter.getItem(i).setImage(R.drawable.fail);
smsStatusAdapter.notifyDataSetChanged();
break;
}
}
};
最佳答案
我认为您应该添加FILL_IN_DATA
或PendingIntent.FLAG_UPDATE_CURRENT
标志:
mSmsManager.sendTextMessage(number, null, getFinalMessage(),
(PendingIntent.getBroadcast(getActivity(), 0, sentIntent,Intent.FILL_IN_DATA)),
(PendingIntent.getBroadcast(getActivity(), 0, deliveredIntent, Intent.FILL_IN_DATA)));
在发送另一条短信之前,请等待收到该短信(
RESULT_OK
):for (int i = 0; i < getSelectedNumbers().size(); i++) {
Log.d("Pete Debug", "Loop value of i = " + String.valueOf(i));
String number = removeSpecialCharacters(getSelectedNumbers().get(i));
String smsSending = "Sending to " + number + "... ";
Log.d("Pete Debug", smsSending);
statusItems = new SmsStatusItems();
statusItems.setMessage(smsSending);
statusItems.setImage(R.drawable.clock);
smsStatusAdapter.add(statusItems);
Intent sentIntent = new Intent(SMS_SENT);
sentIntent.putExtra(SMS_SENT, i);
Intent deliveredIntent = new Intent(SMS_DELIVERED);
deliveredIntent.putExtra(SMS_DELIVERED, i);
// This for waiting ;) put the other code here
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
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));
mSmsManager.sendTextMessage(number, null, getFinalMessage(),
(PendingIntent.getBroadcast(getActivity(), 0, sentIntent,Intent.FILL_IN_DATA)),
(PendingIntent.getBroadcast(getActivity(), 0, deliveredIntent, Intent.FILL_IN_DATA)));
}
关于android - 广播接收器onReceive没有获取Intent bundle ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21919835/