android - getLastLocation() always null after reinstall -


i seem have problem can't figure out. creating application requires location services android. application needs poll location. code had been working months, , when uninstalled phone , reinstalled it, location returning null.

here locationprovider class, grabs location using google api client:

public class locationprovider implements     googleapiclient.connectioncallbacks,     googleapiclient.onconnectionfailedlistener,     locationlistener {  public abstract interface locationcallback {     public void handlenewlocation(location location); }  public static final string tag = locationprovider.class.getsimplename();  /*  * define request code send google play services  * code returned in activity.onactivityresult  */ private final static int connection_failure_resolution_request = 9000;  private locationcallback mlocationcallback; private context mcontext; private googleapiclient mgoogleapiclient; private locationrequest mlocationrequest;   public locationprovider(context context, locationcallback callback) {     mgoogleapiclient = new googleapiclient.builder(context)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build();      mlocationcallback = callback;      // create locationrequest object     mlocationrequest = locationrequest.create()             .setpriority(locationrequest.priority_high_accuracy)              .setinterval(3 * 1000)        // 10 seconds, in milliseconds             .setfastestinterval(1 * 1000); // 1 second, in milliseconds      mcontext = context; }  public void connect() {     mgoogleapiclient.connect(); }  public void disconnect() {     if (mgoogleapiclient.isconnected()) {         locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this);         mgoogleapiclient.disconnect();     } }  @override public void onconnected(bundle bundle) {     log.i(tag, "location services connected.");     location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);      if (location == null) {         //----------------          //----------------          log.i(tag, "couldnt location");         nolocationenableddialog();         /*locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this);         mlocationcallback.handlenewlocation(location); */     }     else {         /* log.i(tag, "couldnt location");         nolocationenableddialog(); */         locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this);         mlocationcallback.handlenewlocation(location);     } }  @override public void onconnectionsuspended(int i) {  }  @override public void onconnectionfailed(connectionresult connectionresult) {     /*      * google play services can resolve errors detects.      * if error has resolution, try sending intent      * start google play services activity can resolve      * error.      */     if (connectionresult.hasresolution() && mcontext instanceof activity) {         try {             activity activity = (activity)mcontext;             // start activity tries resolve error             connectionresult.startresolutionforresult(activity, connection_failure_resolution_request);         /*          * thrown if google play services canceled original          * pendingintent          */         } catch (intentsender.sendintentexception e) {             // log error             e.printstacktrace();         }     } else {         /*          * if no resolution available, display dialog          * user error.          */         log.i(tag, "location services connection failed code " + connectionresult.geterrorcode());     } }  public void nolocationenableddialog(){         alertdialog.builder setlocationdialog = new alertdialog.builder(mcontext);         setlocationdialog.settitle(r.string.location_message_title);         setlocationdialog.setmessage(r.string.location_message);          /*button takes user settings*/         setlocationdialog.setpositivebutton(r.string.affirmative, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 intent settingsintent = new intent(settings.action_location_source_settings);                 mcontext.startactivity(settingsintent);             }         });         /*if no location, close activity*/         setlocationdialog.setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 //do nothing....yet             }         });     setlocationdialog.show(); }  @override public void onlocationchanged(location location) {     mlocationcallback.handlenewlocation(location);     log.i(tag, "new location received "); } }  

i have tried turning on google maps grab location on device, hoping use location grabbed maps. curiously, location icon showed in top bar before reinstalling , no longer shows. know why reinstalling application cause error? running on samsung galaxy s6 running android 6.0.1

thank much!

well, appears have answered own question. serious derp. marshmallow requires individual apps ask permission location , such. since previous installation had different target api, didnt need , manifest sufficed.

i brought target api down 22 , it. in future, need ask permission. question answers quite well:

permission issues location in android marshmallow applicaton


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -