android - getParcelableExtra always null on Pending Intent, but not Intent -
i'm new java let alone android development. have no coding background, thought i'd start 'simple' alarm clock. i'm writing alarm clock, , want update alarms inactive if aren't repeating after go off. when pass parcelable alarmclass (my alarm object) alarm set screen intent, passes fine. when pass same alarmclass intent, , put in pendingintent in alarm manager , try screen can dismiss alarm, alarmclass object i'm calling , populating exact same way null.
here's alarm class:
package com.example.wakeme; import java.util.calendar; import android.os.parcel; import android.os.parcelable; public class alarmclass implements parcelable{ calendar cal = calendar.getinstance(); private long id = 1; private int active = 1; private long time = cal.gettimeinmillis(); private int repeating = 0; private int skipweekends = 0; private int skipweekdays = 0; private int alarmnumber = -1; public long getid(){ return id; } public void setid(long id) { this.id = id; } public int getactive(){ return active; } public void setactive(int active) { this.active = active; } public int getrepeating(){ return repeating; } public void setrepeating(int repeating){ this.repeating = repeating; } public void settime(long time){ this.time = time; } public long gettime(){ return time; } public int getskipweekends(){ return skipweekends; } public void setskipweekends(int skipweekends){ this.skipweekends = skipweekends; } public int getskipweekdays(){ return skipweekdays; } public void setskipweekdays(int skipweekdays){ this.skipweekdays = skipweekdays; } public void setalarmnumber(int alarmnumber){ this.alarmnumber = alarmnumber; } public int getalarmnumber(){ return alarmnumber; } @override public int describecontents(){ return 0; } private alarmclass(parcel in){ this.id = in.readlong(); this.active = in.readint(); this.repeating = in.readint(); this.skipweekdays = in.readint(); this.skipweekends = in.readint(); this.time = in.readlong(); this.alarmnumber = in.readint(); } alarmclass() { return; } public void writetoparcel(parcel out, int flags) { out.writelong(this.id); out.writeint(this.active); out.writeint(this.repeating); out.writeint(this.skipweekdays); out.writeint(this.skipweekends); out.writelong(this.time); out.writeint(this.alarmnumber); } public static final parcelable.creator<alarmclass> creator = new parcelable.creator<alarmclass>(){ public alarmclass createfromparcel(parcel in){ return new alarmclass(in); } public alarmclass[] newarray(int size) { return new alarmclass[size]; } };
from here, can pass object alarm setter update fine:
public void startset(view view){ intent set = new intent(this, setalarm.class); set.putextra("alarm", new alarmclass()); startactivity(set); }
and recieve on time setter right here fine. it's non-null:
public class setalarm extends mainactivity { alarmclass passalarm = new alarmclass(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.alarm_set); setalarmsetlistener(); setdeletelistener(); passalarm = (alarmclass) getintent().getparcelableextra("alarm"); if(passalarm.getalarmnumber() != -1){ timepicker picker = (timepicker) findviewbyid(r.id.timepicker1); calendar passedtime = calendar.getinstance(); passedtime.settimeinmillis(passalarm.gettime()); picker.setcurrenthour(passedtime.get(calendar.hour_of_day)); picker.setcurrentminute(passedtime.get(calendar.minute)); string ampm = (passedtime.get(calendar.hour_of_day) > 12 ? "pm" : "am"); string message = "passed time is: " + (passedtime.get((calendar.hour_of_day)%12)!=0?(passedtime.get(calendar.hour_of_day)%12):12) + ":" + passedtime.get(calendar.minute) + " " + ampm; toast.maketext(getapplicationcontext(), message, toast.length_short).show(); } else{ // nothing } }
but when sent via pendingintent so:
private void newalarm(alarmclass alarmclass){ intent intent = new intent(getbasecontext(), alarm.class); intent.putextra("alarm", alarmclass); calendar cal = calendar.getinstance(); cal.settimeinmillis(alarmclass.gettime()); alarmsdatasource alarm = new alarmsdatasource(this); alarm.open(); alarmclass alarmobject = new alarmclass(); alarmobject = alarm.createalarm(true, cal.gettimeinmillis(), false, false, false); pendingintent torpedo = pendingintent.getbroadcast(this,alarmobject.getalarmnumber(),intent,pendingintent.flag_update_current); alarmmanager gooff = (alarmmanager) getsystemservice(alarm_service); gooff.set(alarmmanager.rtc_wakeup, alarmobject.gettime() ,torpedo); toast.maketext(getapplicationcontext(), "alarm number " + alarmobject.getalarmnumber(), toast.length_long).show(); alarm.close(); }
and received in same way on alarm activity so:
public class boom extends mainactivity{ alarmclass boomalarm = new alarmclass(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); final window wake = getwindow(); wake.addflags(windowmanager.layoutparams.flag_turn_screen_on | windowmanager.layoutparams.flag_dismiss_keyguard); setcontentview(r.layout.boom); settheme(r.style.alarmstyle); overridependingtransition(android.r.anim.fade_in,5000); boomalarm = (alarmclass) getintent().getparcelableextra("alarm"); // vibrator buzz = (vibrator) getsystemservice(vibrator_service); // buzz.vibrate(1000); snoozelistener(); dismisslistener(); }
the passed parcelable object i'm passing boomalarm null.
does know why parcelable object work fine in intent, come out null on other side of pendingintent?
sounds facing issue similar classnotfoundexception when using custom parcelable
there workaround described @ https://code.google.com/p/android/issues/detail?id=6822 , in answer classnotfoundexception when using custom parcelable
Comments
Post a Comment