Android M及以上位置课程和位置罚款返回权限授予真

时间:2016-10-18 05:48:44

标签: android android-6.0-marshmallow android-permissions

我正在尝试访问用户位置,只是整理出权限方面。所以我的理解是,23岁及以上,无论用户是否必须授予许可。

所以我在旧版本的清单中有它:

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

对于较新的&gt; = 23我在显示对话框之前正在进行测试:

// Check for runtime location permissions
private boolean hasRunTimeLocationPermission() {
    int courseLocationPermission = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION);
    return  (courseLocationPermission == PackageManager.PERMISSION_GRANTED );
}

我理解它的方式应该是第一次返回false但它返回true。

用户是否实际上必须禁用位置服务,或者它是否被认为是危险的&#34;它必须第一次被批准?

此外,我正在使用新的模拟器api 23,当我查看位置权限时,它说没有应用程序请求位置。

感谢您的帮助

4 个答案:

答案 0 :(得分:0)

试试这个。

// Check for runtime location permissions
private boolean hasRunTimeLocationPermission() {
    int courseLocationPermission = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION);
    return  (courseLocationPermission == PackageManager.PERMISSION_GRANTED );
}

答案 1 :(得分:0)

尝试这个:

private boolean hasRunTimeLocationPermission() {
 if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED)
        {
            return false;
        }
 else
        {
           return true;
        }
      }

答案 2 :(得分:0)

检查这样的权限: -

Check permissions like this :-

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            //Show Information about why you need the permission
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Need Location Permission");
            builder.setMessage("This app needs location permission.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION_CONSTANT);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else if (permissionStatus.getBoolean(Manifest.permission.ACCESS_COARSE_LOCATION,false)) {
            //Previously Permission Request was cancelled with 'Dont Ask Again',
            // Redirect to Settings after showing Information about why you need the permission
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Need Location Permission");
            builder.setMessage("This app needs location permission.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    sentToSettings = true;
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    Toast.makeText(getBaseContext(), "Go to Permissions to Grant Location", Toast.LENGTH_LONG).show();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else {
            //just request the permission
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION_CONSTANT);
        }

        SharedPreferences.Editor editor = permissionStatus.edit();
        editor.putBoolean(Manifest.permission.ACCESS_COARSE_LOCATION,true);
        editor.commit();


    } else {
        //You already have the permission, just go ahead.
        proceedAfterPermission();
    }

答案 3 :(得分:0)

即使对于api> 23

,也要将此添加到Manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

然后请求这样的运行时权限: -

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

要处理结果,请使用: -

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

      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
        } else {

            // permission denied, boo! 
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
        return;
    }
}
}