android - Service loses it's intent when it is restarted by system -
i starting service
this:
intent intentservice = new intent(getactivity(), acnotificationservice.class); intentservice.putextra("acreservation", reservationactivity.acreservation); getactivity().startservice(intentservice);
and service
class:
/* variables */ private notificationmanager notificationmanager; private countdowntimer countdowntimer; private static int notification_countdown = 0; private static int notification_ranout = 1; private acreservation acreservation; /* callbacks */ @override public void oncreate() { super.oncreate(); // initialize variables notificationmanager = (notificationmanager) getapplicationcontext().getsystemservice(context.notification_service); // register eventbus eventbus.getdefault().register(this); } @override public ibinder onbind(intent intent) { return null; } @override public int onstartcommand(intent intent, int flags, int startid) { // reservation data extras if (acreservation == null) { acreservation = (acreservation) intent.getextras().get("acreservation"); } // start timer countdowntimer = new countdowntimer(acreservation.getremainingmilliseconds(), 100) { @override public void ontick(long millisuntilfinished) { // update/show notification notificationmanager.notify(notification_countdown, createreservationactivenotification((int)timeunit.milliseconds.tominutes(millisuntilfinished))); } @override public void onfinish() { // clear notifications notificationmanager.cancel(notification_countdown); // stop service stopself(); } }.start(); return super.onstartcommand(intent, flags, startid); } /* methods */ public notification createreservationactivenotification(int expinminutes) { notification.builder notification = new notification.builder(getapplicationcontext()); notification.setcontenttitle("reservation"); notification.setcontenttext("your reservation expires in " + expinminutes + " minutes"); notification.setlargeicon(bitmapfactory.decoderesource(getresources(), r.mipmap.ic_launcher)); notification.setsmallicon(r.drawable.car_icon); notification.setongoing(true); notification.setpriority(notification.priority_max); notification.setonlyalertonce(true); return notification.build(); } public void onevent(aceventbusbuttonclick buttonclick) { if (buttonclick.getbuttonaction() != null) { if (buttonclick.getbuttonaction().equals(aceventbusbuttonclick.button_cancel_reservation)) { notificationmanager.cancel(notification_countdown); countdowntimer.cancel(); stopself(); } } }
it's pretty simple. take object out of intent
, use calculate milliseconds. start new countdowntimer
keeps updating notification
.
it works should until service
restarted system. loses it's intent
data , crashes @ line of code when restarted:
if (acreservation == null) { acreservation = (acreservation) intent.getextras().get("acreservation"); }
log says crashes because intent
null
. calling getextras()
on null object when service
restarted.
how can fix this?
return start_redeliver_intent onstartcommand()
instead of super call returns default value.
Comments
Post a Comment