gpt4 book ai didi

android - 广播接收器 onReceive 未获取Intent bundle

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:30 26 4
gpt4 key购买 nike

我正在使用以下代码发送文本(pre-kit kat)。当我创建要传递到 smsManager 的 Intent 时,我将一个额外的 int 放入 Intent 中。在广播接收器中,当我调用 intent.getIntExtra 时,它一直给我一个 0 值。知道为什么吗?

当我把onReceive中的i值打印到log中时,每次都显示值为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_DATAPendingIntent.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com