android - Help wanted regarding Location, Timer and Service -


here code:

main service class

public class simpleservice extends service { timer mytimer; private string provider;  //locationmanager locationmanager; //locationlistener mloclistener; //private handler toasthandler = new handler();  @override public ibinder onbind(intent arg0) {     // todo auto-generated method stub     return null; }  @override public void oncreate() {     super.oncreate();     toast.maketext(this,"service created ...", toast.length_long).show();     mytimer = new timer();     mytimer.schedule(new timertask() {          @override         public void run() {             // todo auto-generated method stub             toasthandler.sendemptymessage(0);           }     },0,1000);  }  @override public void ondestroy() {     super.ondestroy();     toast.maketext(this, "service destroyed ...", toast.length_long).show();     mytimer.cancel(); } private final handler toasthandler=new handler() {     public void handlemessage(message msg)      {         getdata();     }; }; public void getdata() {     locationmanager mlocmanager = (locationmanager) getsystemservice(context.location_service);     locationlistener mloclistener = new mylocationlistener();     mlocmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0,             mloclistener);     // initialize location fields   } public class mylocationlistener implements locationlistener {     @override     public void onlocationchanged(location loc) {         loc.getlatitude();         loc.getlongitude();         string text = "my current location is: " + "latitude = "                 + loc.getlatitude() + "longitude = " + loc.getlongitude();         toast.maketext(getapplicationcontext(), text, toast.length_short)                 .show();         log.d("tag", "starting..");     }      @override     public void onproviderdisabled(string provider) {         toast.maketext(getapplicationcontext(), "gps disabled",                 toast.length_short).show();     }      @override     public void onproviderenabled(string provider) {         toast.maketext(getapplicationcontext(), "gps enabled",                 toast.length_short).show();     }      @override     public void onstatuschanged(string provider, int status, bundle extras) {     } } //end of class mylocationlistener */  } 

here used timer in service call requestlocationupdate() method after each , every 1 second, method doesn't called. when press button ddms tag.d print @ time. output in log cat looks this:

05-11 15:47:29.763: debug/dalvikvm(308): gc_explicit freed 47 objects / 2264 bytes in 59ms 05-11 15:47:34.904: debug/dalvikvm(190): gc_explicit freed 88 objects / 3856 bytes in 151ms 05-11 15:47:36.294: debug/tag(3677): starting.. 05-11 15:47:36.294: debug/tag(3677): starting.. 05-11 15:47:36.294: debug/tag(2449): starting.. 05-11 15:47:36.524: debug/tag(3677): starting.. 05-11 15:47:36.534: debug/tag(3677): starting.. 05-11 15:47:36.544: debug/tag(3677): starting.. 05-11 15:47:36.554: debug/tag(3677): starting.. 05-11 15:47:36.564: debug/tag(3677): starting.. 05-11 15:47:36.574: debug/tag(3677): starting.. 05-11 15:47:36.594: debug/tag(3677): starting.. 05-11 15:47:36.604: debug/tag(3677): starting.. 05-11 15:47:36.614: debug/tag(3677): starting.. 05-11 15:47:36.624: debug/tag(3677): starting.. 05-11 15:47:36.635: debug/tag(3677): starting.. 05-11 15:47:36.644: debug/tag(3677): starting.. 05-11 15:47:36.664: debug/tag(3677): starting.. 05-11 15:47:36.674: debug/tag(3677): starting.. 05-11 15:47:36.694: debug/tag(3677): starting.. 05-11 15:47:36.704: debug/tag(3677): starting.. 05-11 15:47:36.714: debug/tag(3677): starting.. 05-11 15:47:36.734: debug/tag(3677): starting.. 05-11 15:47:44.394: debug/dalvikvm(58): gref has increased 401 05-11 15:47:47.734: debug/dalvikvm(2449): gc_explicit freed 372 objects / 20424 bytes in 149ms 05-11 15:47:53.663: debug/sntpclient(58): request time failed: java.net.socketexception: address family not supported protocol 

can please tell what's going on wrong in code?

you not need use timer request location updates. once registered receive location updates, receive notifications on each location change event.

consider following sample:

public class simpleservice extends service {     timer mytimer;     private string provider;      locationlistener mloclistener = new locationlistener {         @override         public void onlocationchanged(location loc) {             loc.getlatitude();             loc.getlongitude();             string text = "my current location is: " + "latitude = "                 + loc.getlatitude() + "longitude = " + loc.getlongitude();             toast.maketext(getapplicationcontext(), text, toast.length_short)                 .show();             log.d("tag", "starting..");         }          @override         public void onproviderdisabled(string provider) {             toast.maketext(getapplicationcontext(), "gps disabled", toast.length_short).show();         }          @override         public void onproviderenabled(string provider) {             toast.maketext(getapplicationcontext(), "gps enabled",                 toast.length_short).show();         }          @override         public void onstatuschanged(string provider, int status, bundle extras) {         }     } //end of class mylocationlistener */;       @override     public ibinder onbind(intent arg0) {         // todo auto-generated method stub         return null;     }      @override     public void oncreate() {         super.oncreate();         toast.maketext(this,"service created ...", toast.length_long).show();           locationmanager mlocmanager = (locationmanager) getsystemservice(context.location_service);          mlocmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0,             mloclistener );     }      @override     public void ondestroy() {         super.ondestroy();         locationmanager mlocmanager = (locationmanager) getsystemservice(context.location_service);          mlocmanager.removeupdates(mloclistener);         toast.maketext(this, "service destroyed ...", toast.length_long).show();     } } 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -