代码之家  ›  专栏  ›  技术社区  ›  Guilherme Golfetto

BroadcastReceiver中的可序列化Extras

  •  4
  • Guilherme Golfetto  · 技术社区  · 7 年前

    我正在开发一款带有报警功能的Android应用程序。

    到目前为止很好,我正在使用 this tutorial 对于Alarm Manager,但我在使用PendingIntents时遇到了一些问题。

    一切都很正常,直到我尝试根据自己的意图在Extras中发送另一个对象,当它到达另一端时,该对象(witch实现Serializable类)为null。

    这是我的代码:

    Intent intent = new Intent(context, AlarmMedicBroadcastReceiver.class);
    intent.setAction(AlarmeMedicamentoBroadcastReceiver.ACTION_ALARM);
    intent.putExtra("alarm", dto); // <- My Object that extends Serializable
    
    /* Setting 10 seconds just to test*/
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);
    long tempo = cal.getTimeInMillis();
    
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, dto.getId(), intent, 0);
    AlarmManager alarme = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarme.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, tempo, alarmIntent);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        alarme.setExact(AlarmManager.RTC_WAKEUP, tempo, alarmIntent);
    } else {
        alarme.set(AlarmManager.RTC, tempo, alarmIntent);
    }
    

    在我的Receiver类中:

    @Override
    public void onReceive(Context context, Intent intent) {
        if(ACTION_ALARM.equals(intent.getAction())){
    
            AlarmeMedicamentoDTO alarm = (AlarmeMedicamentoDTO) intent.getExtras().getSerializable("alarm");
            /*for this moment, alarm is null*/
    
            /*Other Stuff*/
    
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
    
            notificationBuilder.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setTicker("Company name")
                    //     .setPriority(Notification.PRIORITY_MAX)
                    .setContentTitle("Alarme de Medicamento")
                    .setContentText("Hora de tomar seu medicamento: " + alarm.getNomeMedicamento()) //<- Null Pointer Exception
                    .setContentIntent(pendingIntent)
                    .setContentInfo("Info");
    
            notificationManager.notify(1, notificationBuilder.build());
    
        }
    }
    

    提前感谢

    2 回复  |  直到 7 年前
        1
  •  4
  •   David Wasser    7 年前

    您不能再将自定义对象作为“附加对象”添加到 Intent 你传给的 AlarmManager .在最新版本的Android中 全局定时器 试图解包“extras”,但它不知道如何解包自定义对象,因此这些对象将从“extras”中删除。 您需要将自定义对象序列化为字节数组并将其放入“extras”中,或者将自定义对象存储在其他位置(文件、SQLite DB、静态成员变量等)

        2
  •  0
  •   amirhossein p    4 年前

    必须将自定义对象包装在 Bundle :

    捆绑包装:

    Bundle bundle = new Bundle();
    bundle.putSerializable("myObject", theObject);
    intent.putExtra("myBundle", bundle); // <- Object that extends Serializable
    

    从意图中获取(在接收器中):

    Bundle bundle = intent.getBundleExtra("myBundle");
    YourCustomClass theObject = (YourCustomClass) bundle.getSerializable("myObject"); 
    

    YourCustomClass 可以是实现 Serializable