Android程序在执行某些敏感问题时需要获取权限,比如获取通讯录权限,获取相机权限,获取定位权限等等,针对Android6.0以前,只需要在AndroidManifest.xml文件中使用类似于
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
的代码即可,但是到了Android6.0以后,由于安全机制又有增强,所以需要在代码中动态的获取要是用的权限,所有代码分为两个部分:
申请授权和回调
以下是申请授权
// 拍照的权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(WindDetailEditActivity.this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
} else {
// 需要执行的方法
}
下面是回调
/**
* 动态获取权限回调
*
* @param requestCode 请求码
* @param permissions 权限数组
* @param grantResults 结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 需要执行的方法
} else {
Toast.makeText(this, "请在设置中开启相机权限", Toast.LENGTH_SHORT).show();
}
break;
case 2:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 需要执行的方法
} else {
Toast.makeText(this, "请在设置中开启相册权限", Toast.LENGTH_SHORT).show();
}
break;
}
}
permission列表如下:
1、android.permission.ACCESS_CHECKIN_PROPERTIES
允许读写访问”properties”表在 checkin数据库中,改值可以修改上传( Allows read/write access to the “properties” table in the checkin database, to change values that get uploaded)
2、android.permission.ACCESS_COARSE_LOCATION
允许一个程序访问CellID或WiFi热点来获取粗略的位置(Allows an application to access coarse (e.g., Cell-ID, WiFi) location)
3、android.permission.ACCESS_FINE_LOCATION
允许一个程序访问精良位置(如GPS) (Allows an application to access fine (e.g., GPS) location)
4、android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
允许应用程序访问额外的位置提供命令(Allows an application to access extra location provider commands)
5、android.permission.ACCESS_MOCK_LOCATION
允许程序创建模拟位置提供用于测试(Allows an application to create mock location providers for testing)
6、android.permission.ACCESS_NETWORK_STATE
允许程序访问有关GSM网络信息(Allows applications to access information about networks)
7、android.permission.ACCESS_SURFACE_FLINGER
允许程序使用SurfaceFlinger底层特性 (Allows an application to use SurfaceFlinger’s low level features)
8、android.permission.ACCESS_WIFI_STATE
允许程序访问Wi-Fi网络状态信息(Allows applications to access information about Wi-Fi networks)
9、android.permission.ADD_SYSTEM_SERVICE
允许程序发布系统级服务(Allows an application to publish system-level services).
10、android.permission.BATTERY_STATS
允许程序更新手机电池统计信息(Allows an application to update the collected battery statistics)
11、android.permission.BLUETOOTH
允许程序连接到已配对的蓝牙设备(Allows applications to connect to paired bluetooth devices)
12、android.permission.BLUETOOTH_ADMIN
允许程序发现和配对蓝牙设备(Allows applications to discover and pair bluetooth devices)
13、android.permission.BRICK
请求能够禁用设备(非常危险)(Required to be able to disable the device (very *erous!).)
14、android.permission.BROADCAST_PACKAGE_REMOVED
允许程序广播一个提示消息在一个应用程序包已经移除后(Allows an application to broadcast a notification that an application package has been removed)
15、android.permission.BROADCAST_STICKY