自从Android6.0发布以来,在权限上做出了很大的变动,以前开发者只需要在manifest文件中将权限声明就可以做该权限相关操作了。一直觉得酱紫很不礼貌。比如有的恶意软件突然调起相机拍摄用户头像上传服务器。比如软件会悄悄地访问用户联系人上传服务器。这个隐私的数据怎么能随便拿?
6.0系统以后,危险权限就需要软件询问用户,通过允许后太能够使用该功能。不然呢,我不做申请直接用呢?那就直接崩吧。会报requires android.xxx permission。
普通权限仍然只需要在manifest中声明就可以,而危险权限必须请求用户。
Normal Permissions
普通权限如常用的网络,wifi,设置时间,闹钟,NFC,震动等,
具体如下:
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
Dangerous Permissions
危险权限需涉及到用户的隐私数据或资源,如读取联系人,短信,读取SD卡,打电话,调摄像头,定位等。
权限组
一系列相关权限,如联系人权限组包含 读取、修改、获取账户等权限。
只要用户授权了权限组中的一个,则系统会自动允许该组其他权限。
具体如下:
SMS(短信)
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE(存储卡)
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
CONTACTS(联系人)
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
PHONE(手机)
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
CALENDAR(日历)
READ_CALENDAR
WRITE_CALENDAR
CAMERA(相机)
CAMERA
LOCATION(位置)
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
SENSORS(传感器)
BODY_SENSORS
MICROPHONE(麦克风)
RECORD_AUDIO
使用主要有4个方法:
1.检查是否授予权限
ContextCompat.checkSelfPermission()
2.请求权限:
ActivityCompat.requestPermissions()
3.请求后的回调:
onRequestPermissionsResult()
4.判断用户拒绝权限且选择don't ask again,若勾选提示用户去设置权限
以打电话为例,做法主要步骤:
1、在manifest中申请相关需要权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
2、检查是否授予权限
if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
//权限被拒绝,申请权限
ActivityCompat.requestPermissions(PermissionTestActivity.this,new String[]{Manifest.permission.CALL_PHONE},1);
}else{
//权限同意,拨打电话
callPhone();
}
3.调用打电话
private void callPhone(){
Intent intent = new Intent();
Uri data = Uri.parse("tel:" + "10086");
intent.setData(data);
startActivity(intent);
}
4.请求权限的回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
//权限通过
callPhone();
}else{
Toast.makeText(PermissionTestActivity.this, "权限被拒绝", Toast.LENGTH_SHORT).show();
}
}
}
我们常会在app安装好在欢迎页面统一进行权限申请,该功能应在BaseActivity中封装:
//声明一个数组,将需要的权限都放在其中
String[] permissions = new String[]{
Manifest.permission.CALL_PHONE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//存储哪些权限未被授予
List<String> mPermissionList = new ArrayList<>();
for(int i=0;i<permissions.length;i++){
if(ContextCompat.checkSelfPermission(getApplicationContext(),
permissions[i]) != PackageManager.PERMISSION_GRANTED){
mPermissionList.add(permissions[i]);
}
}
//判断是否为空,不为空的调用ActivityCompate.requestPermisson()授予权限。
if(!mPermissionList.isEmpty()){
String[] requestPermissions = mPermissionList.toArray(new String[mPermissionList.size()]);
ActivityCompat.requestPermissions(this,requestPermissions,REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//权限处理回调中,判断每个权限用户是否授权;用户拒绝授权的,还要判断用户是否勾选 don't ask again,勾选后不再弹框询问
//ActivityCompat.shouldShowRequestPermissionRationale() 默认为ture
if(requestCode == REQUEST_CODE){
for(int i=0;i<grantResults.length;i++){
if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(this,permissions[i]);
if(showRequestPermission){
//打开弹框再次询问
}else{
}
}
}
}
}