我正在使用以下代码发送文本(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;
}
}
};
答案 0 :(得分:3)
我认为你应该添加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)));
}
答案 1 :(得分:1)
一个事件 - 一次性
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
res_int, PendingIntent.FLAG_ONE_SHOT);