android - AlarmManager calls the intent service ASAP rather than at the set time -
i have setexact alarm set in app , when reaches point, instead of activating alarm @ specified time, activates asap!
my alarmmanager code
@targetapi(build.version_codes.m) public void setupalarmmanager(long time) { final intent intentalarm = new intent(this, alarmservice.class); final pendingintent pendingintent = pendingintent.getservice(this, 100, intentalarm, pendingintent.flag_one_shot); final alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); alarmmanager.setexact(alarmmanager.rtc_wakeup, time, pendingintent); final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { alarmmanager.cancel(pendingintent); } }, 1000); log.i(tag, "setupalarmmanager: " + time); }
the handler see if can cancel intent through activity (which doesn't appear working)
my alarmservice
public class alarmservice extends intentservice { private static final string tag = "tag"; // private ringtone ringtonealarm; public alarmservice() { super("alarmservice"); // ringtonealarm = null; log.i(tag, "alarmservice: "); } @override protected void onhandleintent(intent intent) { log.i(tag, "onhandleintent: "); uri alarmtone = ringtonemanager.getdefaulturi(ringtonemanager.type_alarm); ringtone ringtonealarm = ringtonemanager.getringtone(getapplicationcontext(), alarmtone); ringtonealarm.play(); } @override public void ondestroy() { log.i(tag, "ondestroy: "); super.ondestroy(); // ringtonealarm.stop(); } }
can me figure out why alarmmanager calling intent right away instead of @ set time? intent service plays alarm when alarm activates.
also should using intentservice or broadcastreceiver?
to set alarm 32 seconds now, this:
private void setalarmtofuthure(long time){ calendar cal_alarm = calendar.getinstance(); cal_alarm.add(calendar.second, (int)time); // here add desired value current time (for example: 32 seconds) final intent intentalarm = new intent(this, alarmservice.class); final pendingintent pendingintent = pendingintent.getservice(this, 100, intentalarm, pendingintent.flag_one_shot); final alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); alarmmanager.setexact(alarmmanager.rtc_wakeup, cal_alarm.gettimeinmillis(), pendingintent); }
Comments
Post a Comment