在6.0下新的权限机制分为两类,Normal Permissions,这类权限是不需要用户授权,不会触及用户的隐私,只需要在manefest里面申请即可;还有一类是Dangerous Permissions,这类权限需要动态的去申请。
Normal Permission如下:
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS
可以看到Dangerous Permissions是按照组来分类的,如果申请了一组的其中一个权限,那么默认会获得该组的所有权限的使用。
申请的流程
1.在AndroidManifest里面配置需要的权限,无论是普通的还是特殊的权限都需要在这里配置。
2.检查权限,如果没有权限则申请
3.给用户申请权限的解释,这个方法只会在第一次用户拒绝授权,再次去申请这个的权限时会用到
4.申请相应的权限
5.处理申请权限的回调,在这里获得哪些授权成功哪些失败,处理需要的逻辑
具体的例子
下面的例子是获取用户的写外部存储器和打电话的权限
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {//未获取权限
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
//当用户第一次申请拒绝时,再次申请该权限调用
Toast.makeText(this, "拨打电话权限", Toast.LENGTH_SHORT).show();
}
//申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0x01);
} else {//已经获得了权限
call();
}
}
private void call() {
Toast.makeText(this, "打电话", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0x01) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//授权成功
call();
} else {//授权失败
Toast.makeText(this, "获取权限失败", Toast.LENGTH_SHORT).show();
}
}
}
}
这个用于权限的申请,使用起来十分的简单
导入
compile 'pub.devrel:easypermissions:0.1.5'
使用很简单,代码中都有注释,都能看懂
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final String TAG = "lzy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//所要申请的权限
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE};
if (EasyPermissions.hasPermissions(this, perms)) {//检查是否获取该权限
Log.i(TAG, "已获取权限");
} else {
//第二个参数是被拒绝后再次申请该权限的解释
//第三个参数是请求码
//第四个参数是要申请的权限
EasyPermissions.requestPermissions(this, "必要的权限", 0, perms);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//把申请权限的回调交由EasyPermissions处理
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//下面两个方法是实现EasyPermissions的EasyPermissions.PermissionCallbacks接口
//分别返回授权成功和失败的权限
@Override
public void onPermissionsGranted(int requestCode, List perms) {
Log.i(TAG, "获取成功的权限" + perms);
}
@Override
public void onPermissionsDenied(int requestCode, List perms) {
Log.i(TAG, "获取失败的权限" + perms);
}
}
系统权限WRITE_SETTINGS
这段时间项目中一个功能需要修改手机的来电铃音,一直提示没有这个权限,这个权限比较特殊,不像之前一样去申请,按照下面的方式申请
private void CheckPermission() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(this)) {
Toast.makeText(this, getResources().getString(R.string.recorder_permission), Toast.LENGTH_SHORT).show();
Uri selfPackageUri = Uri.parse("package:"
+ ChangeableRecorderActivity.this.getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
selfPackageUri);
startActivity(intent);
}
}
}