How to handle onPermissionResult for ACCESS_FINE_L

2019-07-10 22:46发布

问题:

I'm writing some sort of hello world program for Google Location Services. I already coded most of the stuff; here's the onConnected method:

public void onConnected(Bundle bundle) {

    // Construct the Location Request
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(30000);

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

        // If not, ask for it.
        // On Marshmallow, display permission request dialog
        ActivityCompat.requestPermissions(
                this,
                new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
                LOCATION_REQUEST_NUMBER);
    } else {

        // If already granted, request for location updates
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
}

But I'm kinda stumped on what to do after the user tapped Allow on the dialog.

From what I could gather, I should do this:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
    switch (requestCode) {
        case LOCATION_REQUEST_NUMBER: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                // Great. Now what?

                // I tried the following, but it crashes
                // LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

                // I also tried the following, but it still does nothing.
                mGoogleApiClient.connect();
            } else {
                // Permission Denied.
            }
        }
    }
}

I tried calling requestLocationUpdates again on the Permission Granted, but this happens:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.starleaf1.locationexercise/com.example.starleaf1.locationexercise.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'int com.google.android.gms.location.LocationRequest.b' on a null object reference

ADDED DETAILS:

I want the app to tell a TextView to display the current coordinates. Which I've coded like so:

@Override
public void onLocationChanged(Location location) {
    Log.i(LOG_TAG, "Location has changed.");
    output.setText(location.toString());
}

But when the Allow button is tapped, the app just sits there doing nothing. The app have to be restarted to get it to work.

回答1:

try Following code

private void showPermissionDialog() {
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(mActivity,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(mActivity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_FOR_LOCATION);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    } else {
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_FOR_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (mGoogleApiClient != null)
                    mGoogleApiClient.connect();
                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

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

and use this variable

private static final int MY_PERMISSIONS_REQUEST_FOR_LOCATION = 1;


回答2:

Try this

declare this:

 private static final int REQUEST_CODE_SOME_FEATURES_PERMISSIONS = 124;  

And then

if(android.os.Build.VERSION.SDK_INT>=23) {
                int hasLocationPermission = checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);

            List<String> permissions = new ArrayList<String>();
            if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
            }




            if (!permissions.isEmpty()) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
            }

        }

and

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch ( requestCode ) {
        case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
            for( int i = 0; i < permissions.length; i++ ) {
                if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
                    Log.d( "Permissions", "Permission Granted: " + permissions[i] );
                } else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
                    Log.d( "Permissions", "Permission Denied: " + permissions[i] );
                }
            }
        }
        break;
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}


回答3:

You need two permissions

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

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

            if (mGoogleApiClient != null)
                mGoogleApiClient.connect();
            // permission was granted, yay! Do the
            // contacts-related task you need to do.

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

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


回答4:

  You should check these permission at time of loading your activity so 
  that after permission granting , location instance can be initialize

 Try to use this one as when we are requesting for permission for location,
  we need to request for two permission for ACCESS_FINE_LOCATION and 
  ACCESS_COURSE_LOCATION.

private static final int PERMISSION_REQUEST_CODE = 1;

    private void checkPermission()
    {     

    if(android.os.Build.VERSION.SDK_INT>=23) {
    int result = ContextCompat.checkSelfPermission(YourClass.this, Manifest.permission.ACCESS_FINE_LOCATION);
    List<String> permissions = new ArrayList<String>();

    if( result != PackageManager.PERMISSION_GRANTED ) {
        permissions.add( Manifest.permission.ACCESS_FINE_LOCATION );
        permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    if( !permissions.isEmpty() ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions( permissions.toArray( new String[permissions.size()] ), PERMISSION_REQUEST_CODE );
        }
    }


and then 

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:

            for( int i = 0; i < permissions.length; i++ ) {
                if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
                    Log.d( "Permissions", "Permission Granted: " + permissions[i] );
                 // Here you write your code once permission granted

                    } else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
                    Log.d( "Permissions", "Permission Denied: " + permissions[i] );
                }
            }

            break;
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }
}


回答5:

After experimenting for a while, I managed to get it to work.

Please note that while this solution works in my experiment, I am not sure if the change I did is indeed what got it to work.

What I did was changing mGoogleApiClient.connect() to mGoogleApiClient.reconnect() in the onRequestPermissionResult. I also set it to Override.

Here's how the method finally looks like:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case LOCATION_REQUEST_NUMBER: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if(mGoogleApiClient != null) {
                    mGoogleApiClient.reconnect();
                }
            } else {
                Log.i(LOG_TAG, "Permission denied by user.");
            }
        }
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}