1

I have another problem with location in android 6.0.

I read the official guide to the permissions on Android 6.0 and now work , but my position is null, the code seems blocked !

This is the OnCreate method:

if ( Build.VERSION.SDK_INT >= 23 ) { //CONTROLLO PER ANDROID 6.0 O SUPERIORE
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_ACCESS_FINE_LOCATION);
            }
        }
    }else{
        Log.d("ANDROID","ANDROID SENZA FRONZOLI");
        //dA QUI IN POI MAIN STANDARD
    }

And this is the check permission class:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_ACCESS_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                Log.d("PERMESSI-fine", "PERMESSI OK");

                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

                    ActivityCompat.requestPermissions( this, new String[] {  android.Manifest.permission.ACCESS_FINE_LOCATION  },
                            MY_PERMISSIONS_ACCESS_FINE_LOCATION );
                }

                    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if (location != null) {
                        double longitude = location.getLongitude();
                        double latitude = location.getLatitude();

                        Log.d("GPS-PROVIDER", String.valueOf(longitude));
                        Log.d("GPS-PROVIDER", String.valueOf(latitude));

                        // Toast.makeText(MainActivity.this, "GPS OK", Toast.LENGTH_SHORT).show();
                    }else{
                        Log.d("GPS","NIENTE ZONA VUOTA");
                        Log.d("GPS", String.valueOf(location));
                    }



            } else {


                Log.d("PERMESSI-fine","PERMESSE NO");
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

The code don't give error, simple nothing! null object. Now i'm using the android 6.0 emulator. I don't have the real device with this os version.

Please help me.

1 Answers1

1

emulator don't have gps so location provider won't get any location or any last known location at all.you need to add gps data. follow the link or android.developer source

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68