android - 传值给广播接收者,循环putExtra,

标签 android

简而言之:我有一个 BroadcastReceiver 用于监听 SMS 送达回执。 onReceive 被调用,但我如何识别收据是关于哪条短信的? 将值放入 Intent 中是行不通的,因为如果我同时发送三个 SMS,每次都会有最后一个 SMS 标识符(putExtra() 似乎用相同的键覆盖值)

详细信息:

我有一个监听 GCM 推送通知的类 (MyGcmListenerService)。一种方法将解析来自服务器的 JSON 数据;数据包括一组 ID、短信和数字,例如:

{"texts":[{"id":"11","number":"0494587693","text":"hello"},{"id":"12","number":"0494635478","text":"world"},{"id":"13","number":"0487533693","text":"foo"}]}

对于每条短信,该方法将启动一个发送短信的 IntentService (SendSmsService)。见下面的代码:

JSONObject object = new JSONObject(stringResponse);
JSONArray texts = object.getJSONArray("texts");

for (int i = 0; i < texts.length(); ++i) {
    JSONObject jsonObject = texts.getJSONObject(i);
    int id = jsonObject.getInt("id");
    String number = jsonObject.getString("number");
    String text = jsonObject.getString("text");

    Intent mServiceIntent = new Intent(this, SendSmsService.class);
    startService(mServiceIntent);
}

SendSmsService 中发送短信的代码:

Intent sentIntent = new Intent("com.example.smsgateway.SMS_SENT");
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(
        getApplicationContext(), 0, sentIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);

Intent deliveryIntent = new Intent("com.example.smsgateway.SMS_DELIVERED");
PendingIntent deliverPendingIntent = PendingIntent.getBroadcast(
        getApplicationContext(), 0, deliveryIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);

SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(number, null, message, sentPendingIntent, deliverPendingIntent);

问题 是我有一个监听送货回执的类,该类需要识别送货回执的 id(来自上面的 json 字符串)。

public class SmsDeliveredReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "SMS delivered"); // OK, but which one??

我尝试在 Intent 对象 (putExtra) 中传递 ID: 从 MyGcmListenerServiceSendSmsService:

mServiceIntent .putExtra(SendSmsService.EXTRA_ID, id);

来自 SendSmsService 的接收者:

deliveryIntent.putExtra(EXTRA_ID, id);

但我很快意识到我只能从循环中获取最后的 ID。 putExtra() 覆盖了其他 ID,因为我猜是唯一键。

我想不出这个问题的解决方案,任何帮助将不胜感激

最佳答案

我终于通过 IRC#android-dev 找到了我的问题的解决方案

答案可以在文档中找到: https://developer.android.com/reference/android/app/PendingIntent.html

A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

There are two typical ways to deal with this.

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

我使用了唯一的requestCode

关于android - 传值给广播接收者,循环putExtra,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32669520/

相关文章:

android - 为什么 Android 收不到 FCM 推送通知?

java - 解析 unix,UTC android

android - 为 _utm.gif 文件发送 HTTP 请求后,Google 分析服务器没有响应

android - 将 admob 栏添加到 Recyclerview 的中间。索引越界异常

android - 如何找到我的 Google Play 服务 Android BASE64_PUBLIC_KEY

android - RecyclerView 滚动和高度调节问题

android - 如何在 Android 中使用正则表达式运行查询

java - 再次更改 Android Studio 2.1.1 中的启动器 Logo

android - 为什么在布局 XML 中定义 <shape> 时会出现 ClassNotFoundException?

Android单选按钮无法取消选中