Hi all guys, I know that Google added for firmware 6.0 and above conditional checks, did everything added, works on web except api above 23 ... Why ??? here are the pieces of code and here is my manifesto
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> protected static final int REQUEST_CHECK_SETTINGS = 0x1; private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi; private GoogleApiClient googleApiClient; private LocationRequest locationRequest; locationRequest = new LocationRequest(); locationRequest.setInterval(1000); locationRequest.setFastestInterval(1000); locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); and so
@Override public void onConnected(Bundle bundle) { requestLocationUpdates(); } private void requestLocationUpdates() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(),"Connect on",Toast.LENGTH_LONG).show(); // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onLocationChanged(Location location) { Log.d(LOGSERVICE,"latitud" + location.getLatitude()); Log.d(LOGSERVICE,"lotitude" + location.getLongitude()); myLatitude = location.getLatitude(); myLongitude = location.getLongitude(); lat.setText(String.valueOf(myLatitude)); lot.setText(String.valueOf(myLongitude)); } public void settingsrequest() { LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(1 * 1000); locationRequest.setFastestInterval(1 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); //this is the key ingredient PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can initialize location // requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { // Ignore the error. } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings are not satisfied. However, we have no way to fix the // settings so we won't show the dialog. break; } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { // Check for the integer request code originally supplied to startResolutionForResult(). case REQUEST_CHECK_SETTINGS: switch (resultCode) { case Activity.RESULT_OK: requestLocationUpdates(); Toast.makeText(getApplicationContext(),"Gps turn on",Toast.LENGTH_LONG).show(); break; case Activity.RESULT_CANCELED: settingsrequest();//keep asking if imp or do whatever break; } break; } } @Override protected void onStart() { super.onStart(); settingsrequest(); googleApiClient.connect(); Log.d(LOGSERVICE,"onSTart" ); } @Override protected void onResume() { super.onResume(); if (googleApiClient.isConnected()) { requestLocationUpdates(); } Log.d(LOGSERVICE,"onResume" ); } @Override protected void onPause() { super.onPause(); LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); Log.d(LOGSERVICE,"onPause" ); } @Override protected void onStop() { super.onStop(); googleApiClient.disconnect(); Log.d(LOGSERVICE,"onStop" ); } @Override protected void onDestroy() { super.onDestroy(); Log.d(LOGSERVICE,"onDestroy" ); } }
requestPermissionsand, perhaps, not to initialize the code for working with gps before receiving the perm. And you have todo inrequestLocationUpdateshints at this. - lllyctActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CHECK_SETTINGS);- lllyct