Location loops only above API 23 version Android 6.0(Marshmallow), why?

Asked

Viewed 118 times

0

The problem is that in the above versions (tested on Apis 23, 24, 27) ,(on API 29 ta working) the deburger shows hundreds of passages by onLocationAvailability
'Cause I put a log. This always returns TRUE as follows below in Kitkat API 19 version :

I/locationAvailability :: true
I/Location pos ->: 38.4219983 -122.08430/08/2019 23:21:39
I/locationAvailability :: false
I/Location pos ->: 38.4219983 -122.08430/08/2019 23:22:10
I/locationAvailability :: true
Process 3298 terminated.

But in the versions above API 23 we have :

I/Location Active: true
I/Location Active: true
I/Location Active: true
I/Location Active: true
I/Location Active: true
I/Location Active: true
I/Location Active: true ...... ao infinito.

Nor does it show the latitude and longitude position ! Do I have to use another implementation for versions 23 and above?? I realized that in version 23, 24 and 25 the application receives the permissions, I can see inside the emulator, enabled permissions. But the function returns to act and open the dialog boxes to ask again the permissions.

   protected void onResume() {
    super.onResume();
    addDataHora();
    if(Build.VERSION.SDK_INT >= VERSION_CODES.M) {
        if (!hasPhonePermissions( this, Permissions )) {
            ActivityCompat.requestPermissions( this,Permissions,Permission_All );
        }else{
            TelephonyManager tMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
            if (tMgr != null) {
                @SuppressLint("MissingPermission") String mPhoneNumber = tMgr.getLine1Number();
                phoneNumber = mPhoneNumber;
            }
        }
    }
    int errorcode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable( MainActivity.this );
    switch (errorcode) {
        case ConnectionResult.SERVICE_MISSING:
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
        case ConnectionResult.SERVICE_DISABLED:
            GoogleApiAvailability.getInstance().getErrorDialog( MainActivity.this, errorcode, 0, new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    finish();
                }
            } ).show();
            break;
        case ConnectionResult.SUCCESS:
            break;
    }

    if (ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        return;
    }
    client.getLastLocation().addOnSuccessListener( new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            } else {
                Log.i( "location - ", "null" );
            }
        }
    } ).addOnFailureListener( new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        }
    } );
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setInterval( 20000 );
    locationRequest.setFastestInterval( 10000 ); //location computed for other apps
    locationRequest.setPriority( LocationRequest.PRIORITY_HIGH_ACCURACY ); //uso preciso com gps.
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest( locationRequest );
    SettingsClient settingsClient = LocationServices.getSettingsClient( this );
    settingsClient.checkLocationSettings( builder.build() ).addOnSuccessListener( new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            Log.i( "Location Active ", locationSettingsResponse.getLocationSettingsStates().isNetworkLocationPresent() + "" );
        }
    } ).addOnFailureListener( new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                try {
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult( MainActivity.this, 10 );
                } catch (IntentSender.SendIntentException el) {
                    //todo
                }
            }
        }
    } );
    //Listener pega sempre a nova posição do provider. Kill app se local é nulo.
    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                Log.i( "Local position -> ", "local is null");
                return;
            }
            //procurar na lista de localização.
            for (Location location : locationResult.getLocations()) {
                addDataHora();
                Log.i( "Location pos -> ", location.getLatitude() + " " + location.getLongitude() + DataTime );
            }
        }

        @Override
        public void onLocationAvailability(LocationAvailability locationAvailability) {
            Log.i( "locationAvailability : ", locationAvailability.isLocationAvailable() + "" );
        }
    };
    client.requestLocationUpdates( locationRequest, locationCallback, null );
    Context context = this;
    SharedPreferences sharedPreferences = getSharedPreferences( PREF_NAME,MODE_PRIVATE );
    String tentativas = sharedPreferences.getString( "tentativas","0" );
    atemptcount = Integer.parseInt( tentativas );
 //   Toast.makeText( MainActivity.this, "Load config: "+ tentativas, Toast.LENGTH_LONG).show();
}
//FIM RESUME

 @Override
  protected void onPause() {
    super.onPause();
    if (locationCallback != null) {
        stopLocationUpdate();
    }

/** Remove recursos para liberar memória  */
private void stopLocationUpdate() {
    client.removeLocationUpdates( locationCallback );
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@RequiresApi(api = VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_CODE){
        if(grantResults.length > 0  && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText( MainActivity.this, "Permissão aceita" , Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText( MainActivity.this, "Permissão negada" , Toast.LENGTH_SHORT).show();
        }
    }
}
  • I found a problem with the permissions. In API 29 the permissions are recognized. And the program advances. In version 24 for example, which is more testing. It asks for the validation of permissions, but somehow, they are not computed. And is in a looping function that validates permissions.

  • I believe it is early for you to "close" to ask. To help who will answer too, put the snippet of your function onRequestPermissionsResult

  • I put wryel the problem in version 23 and 25 is much worse, the gps icon is flashing 3 times per second.

  • I installed several times different emulators for api 23, I used a real phone, and it presents the same problem. in api 25 same problem. The other Apis 19, 21,24, and perfect in versions 26,27,28,29. Almost giving up. Too bad most people still use version 23 to 25...

  • In api version 25 the program passes several times in onResume, launches many Logs for me. Showing intense work in this part of the program. things that stable verses don’t do. It would be normal for the program to pass sentences of times by onResume ? It seems to me quite abnormal. He even picks up the location I want, but I’m finding a lot of work at the site.

  • No one wants to help with that answer ?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.