无法在Android

时间:2017-03-03 14:50:20

标签: java android

我想在Android中获得ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION的权限,但无论出于何种原因,它只授予ACCESS_COARSE_LOCATION权限。

我的活动:

final int PERMISSION_ALL = 1;
String[] PERMISSIONS = {ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION};

if(!hasPermissions(this, PERMISSIONS)){
        System.out.println("=========================================nottttt========================================================");
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }



@Override
public void onRequestPermissionsResult(int requestCode, String PERMISSIONS[], int[] grantResults) {
    System.out.println("==================requesttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt");

    switch (requestCode) {
        case PERMISSION_ALL: {
            System.out.println("lengthhhhhhhhhhhhhhhhhhhhhh"+PERMISSIONS.length);
            for (int i = 0; i < PERMISSIONS.length; i++) {
                String permission = PERMISSIONS[i];
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    System.out.println(permission + "is alreadyyyyyyyyyyyyy grantedddddddddddddddd");
                } else {
                    System.out.println(permission + "is not grantedddddddddddddddd");

                }
                return;
            }

        }
    }
}


public static boolean hasPermissions(Context context, String[] permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

Android清单:

<user-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<user-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我启动应用程序,提示我访问该位置,我选择是,但ACCESS_FINE_LOCATION未授予。有人可以帮我找到问题吗?

2 个答案:

答案 0 :(得分:0)

一旦用户授予其中一个权限的权限,另一个权限的权限也将被授予。你不能让其中一个被授予,另一个没有被授予。

答案 1 :(得分:0)

ACCESS_FINE_LOCATION权限允许应用使用GPS和网络位置提供商。但是,网络提供商是一个粗略的位置提供商。这就是为什么,如果您首先请求ACCESS_FINE_LOCATION,它将自动请求ACCESS_COARSE_LOCATION权限