-4

I have a class public class Alarm implements Serializable. This has a method in it:

public void schedule(Context context) {
    setAlarmActive(true);

    Intent myIntent = new Intent(context, AlarmAlertBroadcastReciever.class);

    Bundle bundle = new Bundle();
    bundle.putSerializable("alarm", this);
    myIntent.putExtras(bundle);
    //myIntent.putExtra("alarm", this);

    Alarm alarm = (Alarm) bundle.getSerializable("alarm");

    if(alarm !=null){
        Log.d("Alarmdebug", "alatm is not null");
    }

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);

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

    alarmManager.set(AlarmManager.RTC_WAKEUP, getAlarmTime().getTimeInMillis(), pendingIntent);                 
}

Elsewhere I have this code. Which is not able to get the serialized extras. This is invoked but alarm is always null.

    Bundle bundle = intent.getExtras();

    final Alarm alarm = (Alarm) bundle.getSerializable("alarm");

    if(alarm==null){
        Log.d("Alarmdebug", "alarm is null 2");
    }

Can you please tell me what is the issue with this code? I tried the solution here: Passing data through intent using Serializable but it doesn't work for me.

Thank you

EDIT

Anyone looking for the solution can find it below now.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Umar Ali
  • 13
  • 6

1 Answers1

0

It seems serialized bundles don't work on some devices. Had to to byte[]

Answer is here:

Pass Serializable Object to Pending Intent

Send it like this:

//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);

Receive it like this:

//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();
        }
    }
}
Umar Ali
  • 13
  • 6