我正在使用android Nougat,所以这些答案都没有奏效。我最终将对象传递到字节数组中。
//AlarmService.javaIntent myIntent = new Intent(getApplicationContext(), AlarmalertBroadcastReciever.class);ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutputStream out = null;try { out = new ObjectOutputStream(bos); out.writeObject(alarm); out.flush(); byte[] data = bos.toByteArray(); myIntent.putExtra("alarm", data);} catch (IOException e) { e.printStackTrace();} finally { try { bos.close(); } catch (IOException ex) { ex.printStackTrace(); }}PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getAlarmTime().getTimeInMillis(), pendingIntent);然后我收到了Byte []
//AlarmalertBroadcastReceiver.java@Overridepublic void onReceive(Context context, Intent intent) { ByteArrayInputStream bis = new ByteArrayInputStream(intent.getByteArrayExtra("alarm")); ObjectInput in = null; Alarm alarm = null; try { in = new ObjectInputStream(bis); alarm = (Alarm)in.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } }}


