从API 25在BroadcastReceiver中接收null

时间:2017-05-23 22:42:34

标签: android broadcastreceiver android-7.0-nougat

我发送对象(已实现Serializable)以使用AlarmMananger

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context,AlarmReceiver.class);

    Bundle b = new Bundle();

    Event event = new Event();

    event.setTitle("title");

    b.putSerializable(KEY_EVENT, event);

    intent.putExtras(b);

    int id = 0;

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  id, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT);


        alarmManager.set(AlarmManager.RTC_WAKEUP, interval, pendingIntent);

并在BroadcastReceiver中我收到意图并尝试接收我的自定义对象:

@Override
public void onReceive(Context context, Intent intent) {

    Log.d("TAG", "onReceive: ");

    Bundle b = intent.getExtras();

   Event event = (Event) intent.getExtras().getSerializable(KEY_EVENT);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String title = "";

    if (event != null) {

        if (!TextUtils.isEmpty(event.getTitle())) {
            title = event.getTitle();
        }

    } else {
        Log.d(TAG, "event = null");
    }


}

API之间存在不同的行为,在API 19上,事件对象为非空,在API 25上,事件对象为空。我试图发送原始类型,它没问题,但自定义对象有问题。谁可以帮忙?

0 个答案:

没有答案