android - Service stops updating location -


i working on app has work in background , send location updates server.

the code pretty simple , normaly works. there service has timer sends updates server every 15 seconds , implements locationlistener interface.

i dont think giving code useful, here how set class:

@override public void oncreate() {     super.oncreate();      locationmanager = (locationmanager) getsystemservice(context.location_service);     locationmanager.requestlocationupdates( locationmanager.gps_provider            , 5000, 10.0f, this);     locationmanager.requestlocationupdates( locationmanager.network_provider , 5000, 10.0f, this);      //ping task sends updates server     ping_timer.scheduleatfixedrate( new ping_task(), 5000, ping_task.time_get_jobs*1000 ); } 

in practice have problems code. services should work in background, if service stoppes there gcm system in place restart service in background.

even these protections still have problems, app not update location anymore, if clear service still running. on google maps app can see location correct there, not in app. how can be, why not 'onlocationchanged' event anymore.

thanks help.

first, not sure service life cycle. used code below in onstart() method of service. method called after calling startservice(intent) method on context. guess, can in oncreate() method.

implement location listener:

private final static locationlistener listener = new locationlistener() {      @override     public void onlocationchanged(location location) {         //locationhandler created below.         message.obtain(locationhandler, 0, location.getlatitude() + ", " + location.getlongitude()).sendtotarget();     }      @override     public void onproviderdisabled(string provider) {     }      @override     public void onproviderenabled(string provider) {     }      @override     public void onstatuschanged(string provider, int status, bundle extras) {     } }; 

give listener method instead this

locationmanager.requestlocationupdates( locationmanager.gps_provider, 5000, 10.0f, listener); locationmanager.requestlocationupdates( locationmanager.network_provider, 5000, 10.0f, listener); 

implement handler listener in onstart() method in service.

handler locationhandler = new handler() {     @override     public void handlemessage(android.os.message msg) {         string location = (string) msg.obj;          //do wanna location      } } 

this how did.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -