android - Alarm Manager goes off as i click the Button -
i have code set on button sets alarm manager send broadcast , activate wifi on device. have set delay 1 min code alarm goes off click button
here's code on button:
public void start(view view){ alarmmanager alarm = (alarmmanager) getsystemservice(alarm_service); intent intent1 = new intent(this,myreceiver.class); pendingintent turnwifi = pendingintent.getbroadcast(getapplicationcontext(), 0, intent1, 0); long milli = 60000; alarm.set(alarmmanager.rtc,milli,turnwifi); }
the code in receiver:
public class myreceiver extends broadcastreceiver{ @override public void onreceive(context context, intent intent) { log.i("broadcastreceiver","------------>in on receive method<-------------"); wifimanager wifi_man = (wifimanager) context.getsystemservice(context.wifi_service); wifi_man.setwifienabled(true); }
in alarm.set(alarmmanager.rtc,milli,turnwifi); milli passed
use calendar less complexity.
this should solve problem:
alarm.set(alarmmanager.rtc,system.currenttimemillis() + milli,turnwifi);
update:
explanation:
if stated trigger time in past, alarm triggered immediately
trigger alarmmanager.rtc (real time clock) type time of day represented milliseconds. , 60000 millis literally in past.
according documents, system.currenttimemillis() + 60000 goes off in next 60secs
system.currenttimemillis()
returns current time in milliseconds since january 1, 1970 00:00:00.0 utc.
Comments
Post a Comment