传递使用AlarmManager意图对象 - 接收器获得空对象
问题描述:
你好我试图通过使用下面的代码添加而集AlarmManager对象,传递使用AlarmManager意图对象 - 接收器获得空对象
try {
int when = (int)testDate.getTime();
Intent intent1 = new Intent(mEventService, AlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putSerializable(Contants.NOTIFICATION_DATA, (Serializable) event);
intent1.putExtras(bundle);
PendingIntent sender = PendingIntent.getBroadcast(mEventService, (int)when, intent1, PendingIntent.FLAG_UPDATE_CURRENT); //192837
// Get the AlarmManager service
AlarmManager am = (AlarmManager) mEventService.getSystemService(mEventService.ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP, when, sender);
} catch (ParseException e) {
e.printStackTrace();
}
在接收器类
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Event event = (Event) intent.getSerializableExtra(Contants.NOTIFICATION_DATA);
}
}
在'onReceive'中,getSerializableExtra数据为空。但是,当我通过一个字符串值而不是一个对象它工作正常。 请帮助我找出问题。
答
在PendingIntent
will break things like AlarmManager
on Android 7.0+中使用自定义Parcelable
和Serializable
类别附加项。
'Event'是你的自定义课程?如果是这样,那么它必须由'Serializable'实现。必须检查'事件'不应该'null' – Piyush
@Piyush:我序列化'(可序列化)事件',并且事件不是空对象。 –