Android的7广播接收器的onReceive intent.getExtras丢失数据

人气:712 发布:2022-09-17 标签: broadcastreceiver android-7.0-nougat

问题描述

我的应用程序不工作在Android 7.我BroadcastReceiver.onReceive方法被调用,但intent.getExtras的内容缺失。我已经验证了数据正确。下面是从我的onReceive方法,其中意图作为参数传递的onReceive一个片段。

My app isn't working on Android 7. My BroadcastReceiver.onReceive method is called but the contents of the intent.getExtras is missing. I've verified that the data was correctly loaded. Here's a snippet from my onReceive method, where intent is passed as a parameter to onReceive.

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

文字和phoneNumbersToText都为null。

Both textMessage and phoneNumbersToText are null.

下面是从我的清单文件中的一个片段:

Here's a snippet from my manifest file:

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

在此处,将数据加载一个片段:

Here's a snippet where the data is loaded:

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);

同样的code Android中6工作正常。

The same code works fine in Android 6.

这是什么样的变化有什么想法都在这里需要为Android 7?

Any thoughts on what changes are required here for Android 7?

感谢您

推荐答案

+1,它看起来像您遇到相同的问题我。我登录它跟踪器,( https://开头code.google.com / p /安卓/问题/细节?ID = 216581 ),你发表了评论。

+1, it looks like you're having the same issue as me. I logged it on the tracker, (https://code.google.com/p/android/issues/detail?id=216581) which you commented on.

我的解决方案是使用共享preferences来存储我的自定义对象。然后,当alarmmanager火灾,我运行以下命令以获取对象了。TL;博士,我用GSON序列化/反序列化我的自定义POJO /输出共享preFS作为一个字符串。例如:

My solution was to use SharedPreferences to store my custom object. Then, when the alarmmanager fires, I run the following to get the object out. tl;dr, I use GSON to serialize/deserialize my custom POJO in/out of SharedPrefs as a String. For example:

 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;

希望这有助于你出去!

Hope this helps you out!

471