android - FusedLocationProviderClient getLocationAvailability needs a delay to give a correct result -
i'm trying last location checking first if gps enabled, ask user go settings enable if it's not, , when go settings app, after enabling gps location, , check if gps available, it's not enabled, after delay (1-2 seconds) is.
why there delay in fusedlocationproviderclient.getlocationavailability() method? how can solve it? here's code:
@inject fusedlocationproviderclient fusedlocationproviderclient; @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); checkandgetlastlocation(); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == gps_enable_request) { checkandgetlastlocation(); } else { super.onactivityresult(requestcode, resultcode, data); } } @suppresswarnings("missingpermission") private void checkandgetlastlocation() { fusedlocationproviderclient.getlocationavailability().addoncompletelistener(getactivity(), availabilitylistener -> { if (availabilitylistener.issuccessful() && availabilitylistener.getresult() != null && availabilitylistener.getresult().islocationavailable()) { showprogress(); getlastlocation(); } else { showgpsdisableddialog(); } }); } public void showgpsdisableddialog() { hideprogress(); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle(r.string.gps_disabled_title); builder.setmessage(r.string.gps_disabled_message); builder.setpositivebutton(r.string.enable_gps, (dialog, which) -> { dialog.dismiss(); startactivityforresult(new intent(settings.action_location_source_settings), gps_enable_request); }).setnegativebutton(r.string.not_enable_gps, (dialog, which) -> { switchtomanualmode(); }); alertdialog mgpsdialog = builder.create(); mgpsdialog.setcancelable(false); mgpsdialog.show(); } @suppresswarnings("missingpermission") private void getlastlocation() { locationlistlayout.setvisibility(view.visible); addlocationbutton.setvisibility(view.visible); addlocationlistlayout.setvisibility(view.gone); searchlocationsbutton.setvisibility(view.gone); fusedlocationproviderclient.getlastlocation() .addoncompletelistener(getactivity(), locationlistener -> { if (locationlistener.issuccessful() && locationlistener.getresult() != null) { location lastlocation = locationlistener.getresult(); locationstep1presenter.getsites(lastlocation); } else { log.d(this.gettag(), "checkandgetlastlocation:exception", locationlistener.getexception()); hideprogress(); shownositestoshowerror(); } }); }
Comments
Post a Comment