java - Android: Starting a service at a chosen time -
java - Android: Starting a service at a chosen time -
i have broadcastreceiver starting service. in service have alarmmanager want start service @ time of day, every day.
the broadcastreceiver starts when open app, alarmmanager starts service if start broadcastreceiver @ set time.
how continuously compare nowadays time set time, automatically run when time right?
in mainactivity.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent intent = new intent(); intent.setaction("com.example.broadcastpro.mybroadcastreceiver"); sendbroadcast(intent); }
in mybroadcastreceiver.java
public class mybroadcastreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent i=new intent(context,setperiodicservice.class); context.startservice(i); } }
in setperiodicservice.java
public int onstartcommand(intent intent, int flags, int startid) { calendar calendar = calendar.getinstance(); calendar.settimeinmillis(system.currenttimemillis()); calendar.set(calendar.hour_of_day, 12); calendar.set(calendar.minute, 30); intent i=new intent(this,myservice.class); pendingintent alarmintent = pendingintent.getservice(this, 0, i, 0); alarmmanager alarmmgr = (alarmmanager)getsystemservice(context.alarm_service); alarmmgr.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), alarmmanager.interval_day, alarmintent); homecoming super.onstartcommand(intent, flags, startid); }
in setperiodicservice.java @ alarmmgr.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), alarmmanager.interval_day, alarmintent);
need alter alarmmgr.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), alarmmgr.interval_day, alarmintent);
java android android-service alarmmanager android-calendar
Comments
Post a Comment