android - How to handle exception occur while getting regular location updates in background service? -


i have regular updates of user location. have created simple service flag set start_sticky.

i using googleplayservices locationclient getting regular location updates.

i starting service through homescreen activity.

here code service

import android.app.service; import android.content.intent; import android.location.location; import android.os.bundle; import android.os.ibinder; import android.os.message; import android.os.messenger; import android.os.remoteexception; import android.util.log; import android.widget.toast;  import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesclient; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.location.locationclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest;  public class gpslocationupdatingservice extends service implements                      googleplayservicesclient.connectioncallbacks,                      googleplayservicesclient.onconnectionfailedlistener,                     locationlistener {      private static final string tag = "gpslocationupdatingservice";      public static final int google_play_service_not_available = 1;     public static final int conn_failed_has_resolution = 2;     public static final int show_general_location_detect_prob_dialog = 3;      // milliseconds per second     private static final int milliseconds_per_second = 1000;     // update frequency in seconds     public static final int update_interval_in_seconds = 5;     // update frequency in milliseconds     private static final long update_interval =             milliseconds_per_second * update_interval_in_seconds;     // fastest update frequency, in seconds     private static final int fastest_interval_in_seconds = 1;     // fast frequency ceiling in milliseconds     private static final long fastest_interval =             milliseconds_per_second * fastest_interval_in_seconds;      private locationclient mlocationclient = null;     private locationrequest mlocationrequest = null;     private messenger messenger;       @override     public void oncreate() {         super.oncreate();          mlocationclient = new locationclient(this, this, this);         mlocationrequest = locationrequest.create();          mlocationrequest.setpriority(locationrequest.priority_high_accuracy);         // set update interval 5 seconds         mlocationrequest.setinterval(update_interval);         // set fastest update interval 1 second         mlocationrequest.setfastestinterval(fastest_interval);      }      @override     public int onstartcommand(intent intent, int flags, int startid) {          messenger = (messenger) intent.getextras().get(mainactivity.intent_messenger_extra);          if(servicesconnected()) {             mlocationclient.connect();         }          return start_sticky;     }      @override     public ibinder onbind(intent intent) {         return null;     }      @override     public void onconnected(bundle arg0) {         toast.maketext(this, "connected", toast.length_short).show();          mlocationclient.requestlocationupdates(mlocationrequest, this);     }      @override     public void onconnectionfailed(connectionresult connectionresult) {         if (connectionresult.hasresolution()) {              sendmessageobject(conn_failed_has_resolution, connectionresult.geterrorcode());          } else {              sendmessageobject(show_general_location_detect_prob_dialog, 100);          }      }      @override     public void ondisconnected() {         toast.maketext(this, "disconnected. please re-connect.", toast.length_short).show();     }      @override     public void onlocationchanged(location location) {         string msg = "updated location: " +                 double.tostring(location.getlatitude()) + "," +                 double.tostring(location.getlongitude());         toast.maketext(this, msg, toast.length_short).show();     }      // method used check if gps tracking      // google play service available or not     private boolean servicesconnected() {         // check google play services available         int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this);         // if google play services available         if (connectionresult.success == resultcode) {             // in debug mode, log status             log.d(tag, "google play services available.");             // continue             return true;         // google play services not available reason         } else {             // error code             int errorcode = resultcode;             // send message activity error code.             // show dialog accordingly             sendmessageobject(google_play_service_not_available, errorcode);             // stop service              return false;         }      }      // send message handler activity     private void sendmessageobject(int errortype, int errorcode) {          message msg = message.obtain();          bundle bundle = new bundle();          bundle.putint("errortype", errortype);          bundle.putint("errorcode", errorcode);          msg.setdata(bundle);           try {             messenger.send(msg);         } catch (remoteexception e) {             e.printstacktrace();         }             stopself();     }   } 

as code can see handle:

  • google play services not available exception.
  • for connection failed exception.

i passing message object activity(homeactivity) handle exception.

but not guaranteed activity(homeactvity) visible when exception occur.

so here asking how handle exception when locationclient used in simple background service.

edit: adding mainactivity code

here mainactivity code

public class mainactivity extends fragmentactivity {      private static final string tag = "mainactivity";     public static final string intent_messenger_extra = "messenger";     private static final int connection_failure_resolution_request = 9000;     private static activity activity;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          activity = this;          // start background location detecting web-service         intent intent = new intent(this, gpslocationupdatingservice.class);         intent.putextra(intent_messenger_extra, new messenger(handler1));         startservice(intent);     }      @override     protected void onstart() {         super.onstart();      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      public static activity getactvity() {         return activity;     }      // define dialogfragment displays error dialog     public static class errordialogfragment extends dialogfragment {         // global field contain error dialog         private dialog mdialog;         // default constructor. sets dialog field null         public errordialogfragment() {             super();             mdialog = null;         }         // set dialog display         public void setdialog(dialog dialog) {             mdialog = dialog;         }         // return dialog dialogfragment.         @override         public dialog oncreatedialog(bundle savedinstancestate) {             return mdialog;         }     }      // method calls when returning after updating google play services     @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         // decide based on original request code         switch (requestcode) {              case connection_failure_resolution_request:             /*              * if result code activity.result_ok, try              * connect again              */                 switch (resultcode) {                     case activity.result_ok :                     /*                      * try request again                      */                     break;                 }          }      }       handler handler1 = new handler(new handler.callback() {          @override         public boolean handlemessage(message msg) {              bundle reply = msg.getdata();             int errortype = reply.getint("errortype");             int errorcode;              switch (errortype) {             case gpslocationupdatingservice.google_play_service_not_available:                 // show install google play services dialog                 errorcode = reply.getint("errorcode");                 showinstallplayservicedialog(errorcode);                 break;              case gpslocationupdatingservice.conn_failed_has_resolution:                 // check resolution error code , act accordingly                 errorcode = reply.getint("errorcode");                 getresoltuinandshowdialog(errorcode);                 break;              case gpslocationupdatingservice.show_general_location_detect_prob_dialog:                 // show general dialog of problem                 // detecting location                 showgeneraldialog();                 break;              default:                 break;             }              return false;         }     });      private void showinstallplayservicedialog(int errorcode) {         dialog errordialog = googleplayservicesutil.geterrordialog(                 errorcode,                 activity,                 connection_failure_resolution_request);         // if google play services can provide error dialog         if (errordialog != null) {             // create new dialogfragment error dialog             errordialogfragment errorfragment =                     new errordialogfragment();             // set dialog in dialogfragment             errorfragment.setdialog(errordialog);             // show error dialog in dialogfragment             errorfragment.show(getsupportfragmentmanager(), "location updates");         } else {             // show general dialog of problem             // detecting location             showgeneraldialog();         }     }      private void getresoltuinandshowdialog(int errorcode) {         pendingintent pendingintent = pendingintent.getactivity(getactvity(),                                                                 connection_failure_resolution_request,                                                                 new intent(this, mainactivity.class),                                                                 pendingintent.flag_update_current);          connectionresult connresult = new connectionresult(errorcode, pendingintent);         try {             connresult.startresolutionforresult(mainactivity.getactvity(), connection_failure_resolution_request);         } catch (sendintentexception e) {             e.printstacktrace();              log.d(tag, "get resoltuion , show dialog exception");         }     }      private void showgeneraldialog() {         toast.maketext(getactvity(), "show  general dialog called", toast.length_short).show();     } } 


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -