android - GPS icon disappear when open location popup or the app is killed -


i developing app should send gps data server once every 10 minutes. problem when kill app when gps on, gps icon disappear status bar (while gps still on) , data not sent anymore. if same google maps app icon doesn't disappear. can explain me why happening? here code use

alarmmanager

/**  * alarm_manager_gps_time_interval = 10 min  */ pendingintent mpendingintent = pendingintent             .getservice(                     context,                     pending_intent_request_code_for_gps,                     new intent(context, gpsdataservice.class),                     pendingintent.flag_immutable);     alarmmanager malarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service);     malarmmanager.setinexactrepeating(             alarmmanager.elapsed_realtime_wakeup,             systemclock.elapsedrealtime(),             alarm_manager_gps_time_interval,             mpendingintent); 

service

public class gpsdataservice extends service implements     googleapiclient.connectioncallbacks,     googleapiclient.onconnectionfailedlistener,     locationlistener {  /**  * provides entry point google play services.  */ protected googleapiclient mgoogleapiclient;  /**  * stores parameters requests fusedlocationproviderapi.  */ protected locationrequest mlocationrequest;  /**  * represents geographical location.  */ protected location mcurrentlocation;  private string mpayload; private gpspojobuilder mgpspojobuilder;  @override public void oncreate() {     super.oncreate(); }  @override public int onstartcommand(intent intent, int flags, int startid) {       if(mgoogleapiclient == null)         buildgoogleapiclient();      mgoogleapiclient.connect();      if (mgoogleapiclient.isconnected())         startlocationupdates();      return service.start_not_sticky; } protected synchronized void buildgoogleapiclient() {      mgoogleapiclient = new googleapiclient.builder(this)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build();      createlocationrequest(); } /**  * update_interval_in_milliseconds = 4 sec  * fastest_update_interval_in_milliseconds = 2 sec  */ protected void createlocationrequest() {     mlocationrequest = new locationrequest();     mlocationrequest             .setinterval(update_interval_in_milliseconds)             .setfastestinterval(fastest_update_interval_in_milliseconds)             .setpriority(locationrequest.priority_high_accuracy); }  @override public void onconnected(@nullable bundle bundle) {     startlocationupdates(); } @override public void onconnectionsuspended(int i) {     // connection google play services lost reason. call connect()     // attempt re-establish connection.     mgoogleapiclient.connect(); } @override public void onconnectionfailed(@nonnull connectionresult connectionresult) { } /**  * requests location updates fusedlocationapi.  */ protected void startlocationupdates() {     checkforpermission(); } private void checkforpermission() {      location location;      if (build.version.sdk_int >= 23) {          if (contextcompat.checkselfpermission(getapplicationcontext(),                 manifest.permission.access_fine_location) != packagemanager.permission_granted) {             //permission denied         } else {             location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);             if (location != null)                 handlelocation(location);             else                 locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this);         }     }else {         location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);         if(location != null)             handlelocation(location);         else             locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this);     } } @override public void onlocationchanged(location location) {     handlelocation(location); } private void handlelocation(location location){      mcurrentlocation = location;      toast.maketext(this, "location foound: " + location.tostring(), toast.length_short).show();      thread thread = new thread() {         @override         public void run() {             try {                 mgpspojobuilder = new gpspojobuilder(getapplicationcontext());                 mpayload = mgpspojobuilder                         .getcheckinjson(mcurrentlocation.getlatitude(), mcurrentlocation.getlongitude());                 sendjson(mpayload, checkins_url);             } catch (googleplayservicesrepairableexception e) {                 e.printstacktrace();             } catch (googleplayservicesnotavailableexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             } {                 if(mgoogleapiclient.isconnected()) {                     stoplocationupdates();                     mgoogleapiclient.disconnect();                 }             }         }     };     thread.start(); } public void sendjson(string payloaddata, string url_suffix) {     httpconnectioncreator mhttpconncectionclass = new httpconnectioncreator(payloaddata, url_suffix);     mhttpconncectionclass.connect(); } /**  * removes location updates fusedlocationapi.  */ protected void stoplocationupdates() {                locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); }  @nullable @override public ibinder onbind(intent intent) {     return null; }  @override public void ondestroy() {      if (mgoogleapiclient.isconnected()) {         stoplocationupdates();         mgoogleapiclient.disconnect();     }     super.ondestroy();   } } 

according android.developer, if want keep services alive, need use start_sticky.

int start_sticky constant return onstartcommand(intent, int, int): if service's process killed while started (after returning onstartcommand(intent, int, int)), leave in started state don't retain delivered intent. later system try re-create service.

inside following function:

@override public int onstartcommand(intent intent, int flags, int startid) {   if(mgoogleapiclient == null)     buildgoogleapiclient();  mgoogleapiclient.connect();  if (mgoogleapiclient.isconnected())     startlocationupdates();  return service.start_not_sticky; 

}

change this:

@override public int onstartcommand(intent intent, int flags, int startid) {   if(mgoogleapiclient == null)     buildgoogleapiclient();  mgoogleapiclient.connect();  if (mgoogleapiclient.isconnected())     startlocationupdates();  return service.start_sticky; 

}


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -