如何正确处理 ACCESS_FINE_LOCATION 的 onPermissionResult?

How to handle onPermissionResult for ACCESS_FINE_LOCATION properly?

我正在为 Google 定位服务编写某种 hello world 程序。我已经编写了大部分内容;这是 onConnected 方法:

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);
    }
}

但在用户点击对话框上的 Allow 后,我有点不知道该怎么办。

据我所知,我应该这样做:

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.
            }
        }
    }
}

我尝试在 Permission Granted 上再次调用 requestLocationUpdates,但发生了这种情况:

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

添加了详细信息

我想让应用告诉 TextView 显示当前坐标。我这样编码:

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

但是当 Allow 按钮被点击时,应用程序只是坐在那里什么都不做。该应用程序必须重新启动才能运行。

试试下面的代码

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
    }
}

并使用这个变量

private static final int MY_PERMISSIONS_REQUEST_FOR_LOCATION = 1;

试试这个

声明:

 private static final int REQUEST_CODE_SOME_FEATURES_PERMISSIONS = 124;  

然后

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);
            }

        }

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);
        }
    }
}

您需要两个权限

  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
}
  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);
        }

    }
}

经过一段时间的试验,我设法让它工作了。

请注意,虽然此解决方案在我的实验中有效,但我不确定我所做的更改是否确实使它起作用。

我所做的是在 onRequestPermissionResult 中将 mGoogleApiClient.connect() 更改为 mGoogleApiClient.reconnect()。我也设置为Override.

方法最终如下所示:

@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);
}