如何正确处理ACCESS_FINE_LOCATION的onPermissionResult?

时间:2016-06-03 09:35:15

标签: android android-permissions

我正在为Google Location Services编写某种 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);
    }
}

但是,在用户点击对话框中的允许之后,我有点难以做些什么。

从我能收集到的,我应该这样做:

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

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

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

但是当点击允许按钮时,应用程序就在那里无所事事。必须重新启动该应用才能使其正常工作。

5 个答案:

答案 0 :(得分:0)

尝试以下代码

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;

答案 1 :(得分:0)

试试这个

声明:

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

答案 2 :(得分:0)

您需要两个权限

  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
}

答案 3 :(得分:0)

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

    }
}

答案 4 :(得分:0)

经过一段时间的实验,我设法让它发挥作用。

请注意,虽然此解决方案适用于我的实验,但我不确定如果我所做的更改确实能让它发挥作用。

我所做的是将mGoogleApiClient.connect()更改为mGoogleApiClient.reconnect()中的onRequestPermissionResult。我还将其设置为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);
}