【问题标题】:Android 7 BroadcastReceiver onReceive intent.getExtras missing dataAndroid 7 BroadcastReceiver onReceive intent.getExtras 缺少数据
【发布时间】:2016-12-11 01:06:01
【问题描述】:

我的应用无法在 Android 7 上运行。我的 BroadcastReceiver.onReceive 方法被调用,但 intent.getExtras 的内容丢失。我已验证数据已正确加载。这是我的 onReceive 方法中的一个 sn-p,其中意图作为参数传递给 onReceive。

Bundle bundle = intent.getExtras();
textMessage = bundle.getString("TEXT_MESSAGE");
ArrayList<MyPhoneNumber> phoneNumbersToText = bundle.getParcelableArrayList("PHONE_NUMBERS");

textMessage 和 phoneNumbersToText 均为 null。

这是我的清单文件中的一个 sn-p:

<receiver android:process=":remote" android:name="com.friscosoftware.timelytextbase.AlarmReceiver"></receiver> 

这是加载数据的sn-p:

Intent intent = new Intent(context , AlarmReceiver.class);  
intent.putExtra(Constants.TEXT_MESSAGE, scheduledItem.getMessageToSend());
intent.putExtra(Constants.PHONE_NUMBERS, scheduledItem.getPhoneNumbersToText());    

PendingIntent sender = PendingIntent.getBroadcast(context, getRequestCodeFromKey(key), intent, PendingIntent.FLAG_UPDATE_CURRENT);

// Get the AlarmManager service
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, selectedDateTime.getTimeInMillis(), sender);

相同的代码在 Android 6 中运行良好。

您对 Android 7 需要进行哪些更改有任何想法吗?

谢谢

【问题讨论】:

  • Intent 有 setAction(String action) 方法。您可以在此处粘贴可序列化对象。有用。 Set extras 在 Android 7 中不起作用。我遇到了同样的问题。这是示例 - stackoverflow.com/a/54718479/4516797

标签: broadcastreceiver android-7.0-nougat


【解决方案1】:

+1,看来你和我有同样的问题。我将其记录在您评论过的跟踪器 (https://code.google.com/p/android/issues/detail?id=216581) 上。

我的解决方案是使用 SharedPreferences 来存储我的自定义对象。然后,当警报管理器触发时,我运行以下命令将对象取出。 tl;博士,我使用 GSON 将我的自定义 POJO 序列化/反序列化为字符串输入/输出 SharedPrefs。例如:

 String json = getSharedPrefs(context).getString(NotificationUtility.NEXT_REMINDER_KEY, "No reminder found");
    try {
        Gson gson = new Gson();
        Reminder reminder = gson.fromJson(json, Reminder.class);
        if (reminder != null) {
            return reminder;
        }
    } catch (Exception error) {
        Log.i(TAG, "Error parsing json: " + error.getMessage(), error);
        return null;
    }
    return null;

希望对你有所帮助!

【讨论】:

  • 谢谢,我会试试你的建议。希望会尽快修复。
  • 您的解决方案有效,但出于其他原因,我决定将数据存储在数据库中并简单地传递密钥。
  • 所以根据错误报告它的预期行为......等等什么?
【解决方案2】:

我遇到了类似的问题,但我想我找到了一个简单的解决方案。将您的数据放入一个 Bundle 中,然后发送带有警报意图的 Bundle。就我而言,我想根据自己的意图发送一个可序列化的对象。

设置闹钟:

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
`
Intent intent = new Intent(context, AlarmReciever.class);
Bundle bundle = new Bundle();

// creating an example object
ExampleClass exampleObject = new ExampleClass();

// put the object inside the Bundle
bundle.putSerializable("exapmle", exampleObject);

// put the Bundle inside the intent
intent.putExtra("bundle",bundle);

PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// setup the alarm
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

收到警报:

public class AlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // get the Bundle
        Bundle bundle = intent.getBundleExtra("bundle");
        // get the object
        ExampleClass exampleObject = (ExampleClass)bundle.getSerializable("example");
    }

}

对我来说效果很好。希望对你有帮助:)

【讨论】:

  • 看起来很有希望,但由于数据已经存储在数据库中,我决定简单地传递密钥。谢谢
  • 也为我工作,谢谢。非常清晰和直截了当。
【解决方案3】:

Android O 版本无法在 BroadcastReceivers 中正确获取额外功能。但一个很好的解决方案是使用意图的 setAction(String action) 方法来发送可序列化的 Alarm 对象。然后将其返回到 onReceive 中的对象。 这是示例:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction(new Gson().toJson(alarm));

然后在你的闹钟接收器中

public void onReceive(Context context, Intent intent) {

    String alarmSerializable = intent.getAction();
    if (alarmSerializable != null)
        alarm = new Gson().fromJson(alarmSerializable, Alarm.class);

    //TODO next, send notification
}

这是 Android O 和​​其他人常用的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2017-05-17
    相关资源
    最近更新 更多