i starting background service mainactivity startservice(intent) sends location of user server. want enable user stops service (also stoping requestlocationupdates of firing data onlocationchanged method) if not wish clicking button in menu. have tried code in onoptionsitemselected method service still works when click button.
how can stop service?
mainactivity:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.route_available); intent = new intent(this, trackingservice.class); startservice(i); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.disconnect_id: system.out.println("test oncreateoptionsmenu invoked."); // stop service when menu button clicks. intent = new intent(this, trackingservice.class); stopservice(i); return true; default: return super.onoptionsitemselected(item); } } tracking service class:
public class trackingservice extends service { .... @override public int onstartcommand(intent intent, int flags, int startid) { // todo auto-generated method stub toast.maketext(this, "location updation has started", toast.length_long) .show(); log.v("x", "response:in onstartcommand()"); detectlocation(); stopself(); return start_sticky; } private void detectlocation() { // todo auto-generated method stub toast.maketext(this, "inside detectlocation()", toast.length_short) .show(); locationmanager lm = (locationmanager) getsystemservice(context.location_service); locationlistener ll = new mylocationlistener(this); // location updates: @ least 0 meter , 60 seconds change. lm.requestlocationupdates(locationmanager.gps_provider, 30 * 1000, 0, ll); log.v("x", "response:after lm1.requestlocationupdates "); enablegps(lm); } }
it looks way have coded now, service isn't doing anything.
because have registered mylocationlistener instance locationlistener, stopping service doesn't anything.
also, since call stopself() in onstartcommand(), you're stopping service every time starts.
i recommend making service locationlistener, , don't call stopself() in onstartcommand() . also, override ondestroy() in service, , explicitly call removeupdates() in order ensure app releases it's request keep gps radio active.
something this:
public class trackingservice extends service implements locationlistener{ locationmanager lm; //make instance variable //.... @override public int onstartcommand(intent intent, int flags, int startid) { // todo auto-generated method stub toast.maketext(this, "location updation has started", toast.length_long) .show(); log.v("x", "response:in onstartcommand()"); detectlocation(); //stopself(); //don't stop service here return start_sticky; } @override public void ondestroy() { super.ondestroy(); lm.removeupdates(this); } @override public ibinder onbind(intent intent) { return null; } private void detectlocation() { // todo auto-generated method stub toast.maketext(this, "inside detectlocation()", toast.length_short) .show(); lm = (locationmanager) getsystemservice(context.location_service); //locationlistener ll = new mylocationlistener(this); // location updates: @ least 0 meter , 60 seconds change. lm.requestlocationupdates(locationmanager.gps_provider, 30 * 1000, 0, this); log.v("x", "response:after lm1.requestlocationupdates "); enablegps(lm); } @override public void onlocationchanged(location location) { //put location changed code here } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } }
Comments
Post a Comment