android - FusedLocationApi.getLastLocation returns null -
i have singleton location service
public class locationservice implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener { public static final string tag = locationservice.class.getsimplename(); private static locationservice instance; private locationrequest mlocationrequest; private googleapiclient mgoogleapiclient; public location location; private context context; private locationservice (context context) { this.context = context; mgoogleapiclient = new googleapiclient.builder(context) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); mgoogleapiclient.connect(); mlocationrequest = locationrequest.create() .setpriority(locationrequest.priority_high_accuracy) .setinterval(10 * 1000) // 10 seconds, in milliseconds .setfastestinterval(60 * 1000); // 1 second, in milliseconds } public static locationservice getinstance(context context){ if (null == instance){ instance = new locationservice(context); } return instance; } @override public void onconnected(bundle bundle) { log.i(tag, "location services connected."); if (activitycompat.checkselfpermission(this.context, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this.context, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { return; } location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); this.location = location; if (location != null) { log.d(tag, location.tostring()); } } @override public void onconnectionsuspended(int i) { log.i(tag, "location services suspended. please reconnect."); } @override public void onconnectionfailed(connectionresult connectionresult) { log.i(tag, "location services connection failed code " + connectionresult.geterrorcode()); } public void onlocationchanged(location location) { this.location = location; log.i(tag, "location changed."); log.i(tag, location.tostring()); } } i start in mainactivity calling in oncreate
locationservice locationservice = locationservice.getinstance(getapplicationcontext()); but location null , onlocationchanged doesnt called
onconnected being called, checked in debugger, location inside null
what do wrong ?
if last location null need register updates @ least 1 time first last location.
check documentation for "request location updates".
Comments
Post a Comment