标签:
BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
Android6.0分了几种权限:Normal Permissions(安装时自动授权,用户也不能取消权限) andDangerous Permissions
<!-- Android6.0 蓝牙扫描才需要--> <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
//判断是否有权限 // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) //请求权限 ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION); //判断是否需要 向用户解释,为什么要申请该权限 ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS) //权限申请结果 onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
//如果使用ActivityCompat.requestPermissions,不会调用onRequestPermissionsResult() //请求权限 requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION); //判断是否需要 向用户解释,为什么要申请该权限 shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)
android.permission.WRITE_SETTINGS不能自动授权,也不能运行时请求授权,咋整啊?通过打开Intent来让用户设置。貌似SETTINGS的权限只能这么处理,from CommonsWare research Android 6.0 changes.
/** * An app can use this method to check if it is currently allowed to write or modify system * settings. In order to gain write access to the system settings, an app must declare the * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its manifest. If it is * currently disallowed, it can prompt the user to grant it this capability through a * management UI by sending an Intent with action * {@link android.provider.Settings#ACTION_MANAGE_WRITE_SETTINGS}. * * @param context A context * @return true if the calling app can write to system settings, false otherwise */ if(!Settings.System.canWrite(this)){ Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE) { if (Settings.System.canWrite(this)) { //检查返回结果 Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission granted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission not granted", Toast.LENGTH_SHORT).show(); } } }
来自:https://yanlu.me/android-m6-0-permission-chasm/
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/5778770.html