java - Android alarm manager does not wait -
i try start simple method every 20 seconds, idea start alarm in method again. create class again, in method executed , starting alarm... , on method should create notification.
public class createnotification extends broadcastreceiver{ public void onreceive(context context, intent intent) { dostuff(); notificationcompat.builder mnotebuilder = new notificationcompat.builder(context) .setsmallicon(r.drawable.icon) .setcontenttitle("...") .setcontenttext(shownstring) //get instance of notificationmanager service notificationmanager mnotifymgr = (notificationmanager) context.getsystemservice(context.notification_service); //build notification mnotifymgr.notify(mnotificationid, mnotebuilder.build()); createnewalarm(context); } private void createnewalarm(context context){ intent alarmintent = new intent(context, createnotification.class); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, alarmintent, 0); alarmmanager manager = (alarmmanager) context.getsystemservice(context.alarm_service); manager.set(alarmmanager.rtc_wakeup, 20000, pendingintent); } }
this started in main activity:
intent alarmintent = new intent(this, createnotification.class); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, alarmintent, 0); alarmmanager manager = (alarmmanager) getsystemservice(alarm_service); manager.set(alarmmanager.rtc_wakeup, 4000, pendingintent);
now problem is, don't expected result, new notification every 20 seconds, creates time notification, fast processor allows. there no break in between , alarmmanager seems not schedule anything, creates class instantly.
thanks lot help!
manager.set(alarmmanager.rtc_wakeup, system.currenttimemillis()+20000, pendingintent);
your solution: 20000 = 1970. january 1. 0:00:20 have add millisec current time. (another solution current time.)
calendar calendar = calendar.getinstance(); calendar.gettimeinmillis()+yourtimeinmillis;
Comments
Post a Comment