Android 7 BroadcastReceiver onReceive intent.getExtras缺少数据

问题描述 投票:5回答:3

我的应用程序无法在Android 7上运行。我调用了BroadcastReceiver.onReceive方法,但缺少intent.getExtras的内容。我已经验证数据已正确加载。这是我的onReceive方法的一个片段,其中intent作为参数传递给onReceive。

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

textMessage和phoneNumbersToText都为null。

这是我的清单文件中的一个片段:

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

这是加载数据的片段:

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需要进行哪些更改的任何想法?

谢谢

broadcastreceiver android-7.0-nougat
3个回答
1
投票

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

我的解决方案是使用SharedPreferences来存储我的自定义对象。然后,当alarmmanager触发时,我运行以下命令将对象取出。 tl; dr,我使用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;

希望这可以帮助你!


3
投票

Android O版本在BroadcastReceivers中没有正确获取额外内容。但是一个很好的解决方案是使用intent的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和其他人的常用方式。


0
投票

我有类似的问题,但我认为我找到了一个简单的解决方案。将您的数据放入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");
    }

}

它对我来说很好。希望能帮助到你 :)

© www.soinside.com 2019 - 2024. All rights reserved.