Android 7 BroadcastReceiver onReceive intent.getExtras 缺少数据
Android 7 BroadcastReceiver onReceive intent.getExtras missing data
我的应用程序无法在 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 需要进行哪些更改的任何想法?
谢谢
+1,看来你遇到了和我一样的问题。我在您评论过的跟踪器 (https://code.google.com/p/android/issues/detail?id=216581) 上记录了它。
我的解决方案是使用 SharedPreferences 来存储我的自定义对象。然后,当 alarmmanager 触发时,我 运行 以下命令将对象取出。
tl;dr,我使用 GSON 将 SharedPrefs 的 serialize/deserialize 我的自定义 POJO in/out 作为字符串。例如:
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;
希望对您有所帮助!
我遇到了类似的问题,但我想我找到了一个简单的解决方案。将您的数据放入一个 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");
}
}
对我来说效果很好。希望对您有所帮助:)
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等人的常用方式
我的应用程序无法在 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 需要进行哪些更改的任何想法?
谢谢
+1,看来你遇到了和我一样的问题。我在您评论过的跟踪器 (https://code.google.com/p/android/issues/detail?id=216581) 上记录了它。
我的解决方案是使用 SharedPreferences 来存储我的自定义对象。然后,当 alarmmanager 触发时,我 运行 以下命令将对象取出。 tl;dr,我使用 GSON 将 SharedPrefs 的 serialize/deserialize 我的自定义 POJO in/out 作为字符串。例如:
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;
希望对您有所帮助!
我遇到了类似的问题,但我想我找到了一个简单的解决方案。将您的数据放入一个 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");
}
}
对我来说效果很好。希望对您有所帮助:)
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等人的常用方式