12

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

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

textMessage 和 phoneNumbersToText 都为空。

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

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

谢谢

4

3 回答 3

9

Android O 版本无法在 BroadcastReceivers 中正确获得额外功能。但一个很好的解决方案是使用意图的setAction(String action)方法来发送可序列化的警报对象。然后将其返回给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和其他人的常用方式。

于 2019-02-15T23:49:40.233 回答
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;

希望这可以帮助你!

于 2016-08-05T17:26:54.250 回答
1

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

}

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

于 2017-06-07T10:21:15.300 回答