将Serializable对象传递给Pending Intent
我试图将一个可序列化的对象发送到挂起的Intent。 问题是收到的警报返回为null。 即使Alarm实现了可序列化的接口。
//AlarmService.java Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class); Bundle bundle = new Bundle(); bundle.putSerializable("alarm", alarm); myIntent.putExtras(bundle); 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);
收到的警报为空。
//AlarmAlertBroadcastReceiver.java public class AlarmAlertBroadcastReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Alarm alarm = (Alarm)intent.getExtras().getSerializable("alarm"); } }
编辑:我尝试过的更多内容如下,但它似乎不起作用:
//AlarmService.java Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class); myIntent.putExtra("alarm", alarm); myIntent.setAction("abc.xyz"); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getAlarmTime().getTimeInMillis(), pendingIntent);
收到的警报为空。
//AlarmAlertBroadcastReceiver.java public class AlarmAlertBroadcastReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Alarm alarm = (Alarm)intent.getExtras().getSerializable("alarm"); //Alarm alarm = (Alarm)intent.getSerializableExtra("alarm"); } }
将自定义对象放入Intent
然后传递给AlarmManager
或NotificationManager
或其他外部应用程序存在已知问题。 您可以尝试将自定义对象包装在Bundle
,因为这有时会起作用。 例如,将代码更改为:
Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class); Bundle bundle = new Bundle(); bundle.putSerializable("alarm", alarm); myIntent.putExtra("bundle", bundle);
并在AlarmAlertBroadcastReciever.onReceive()
:
Bundle bundle = intent.getBundleExtra("bundle"); if (bundle != null) { Alarm alarm = (Alarm)bundle.getSerializable("alarm"); }
如果这不起作用(这应该适用于大多数Android版本/设备,但不是全部,特别是非常新的),您需要将Serializeable
对象转换为byte[]
并将byte[]
放入其他内容中。
有很多关于如何在Stackoverflow上执行此操作的示例。
我正在使用android Nougat,所以这些答案都没有奏效。 我最终以字节数组传递对象。
//AlarmService.java Intent 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 @Override public 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(); } } }
Intent putExtra(String name,Parcelable value)
将扩展数据添加到intent。 名称必须包含包前缀,例如app com.android.contacts将使用“com.android.contacts.ShowAll”之类的名称。
参考
你在Bundle中有putSerializable
。 而且你试图从意图中获得getSerializable
。
For set : `myIntent.putExtra("alarm", alarm);` For get : `(Alarm)intent.getExtras().getSerializableExtra("alarm");` Also make sure your `Alarm` calss have `implements` with `Serializable`.
试试看。
这是一个建议,你可以尝试parcelabler,这是更有效和针对Android特定需求量身定制的。 所以让你的模型类在这里被包围。 之后粘贴到您的项目中,然后使用任一捆绑将其传递给另一个活动
bundle.putParcelable(KEY_PARCEL, detailModelsList.get(position));
要么
intent.putParcelableArrayListExtra("Parcelaber",myList);
有两件事我想建议:
- 您需要将
Action
设置为您的意图,例如:yourIntent.setAction("abc.xyz")
- 您应该使用
FLAG_UPDATE_CURRENT
而不是FLAG_CANCEL_CURRENT
希望能帮助到你。
您是否尝试过除0以外的任何其他请求ID
例如
int requestID = (int) System.currentTimeMillis(); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID , myIntent, PendingIntent.FLAG_UPDATE_CURRENT); public void onReceive(Context context, Intent intent) { if(intent!=null && intent.hasExtra("alarm")){ Alarm alarm = (Alarm)intent.getExtras().getSerializable("alarm"); } }
- 我无法在SlidingMenu片段中单击ListView,不会出现布局
- overridePendingTransition仅显示输入动画
- 像位图一样保存视图,我只得到黑屏
- 导入JAR后的Android java.lang.NoClassDefFoundError
- 最终关键字如何在Android中运行
- 片段不会出现在扩展ActionBarActivity的活动中
- 在VIewPager(Android)中的两个片段之间传递数据(NullPointerException)
- 在Android Java应用程序中使用OpenCV Mat。 OpenCV Mat的NoClassDefFound错误
- connect()和disconnect()在哪里实现?