如果是在6.0以下或者是目标sdk版本在23一下只需要在Mainifest中声明权限就好了
android版本在6.0以上或者目标sdk版本在23以上,就需要动态申请危险权限包括:
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
动态调用权限代码:(写数据为例)
在这里插入代码片`在这里插入代码片
`//权限动态申请
public void requestAllPower() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {//如果应用没有权限
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//判断是否之前禁止过权限,禁止过就不会重新要求申请权限
Toast.makeText(this, "之前禁止了!", Toast.LENGTH_SHORT).show();
}
else {
//之前没申请过的话就申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
//获得权限
}
}
}
回调方法:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "" + "权限" + permissions[i] + "申请成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "" + "权限" + permissions[i] + "申请失败", Toast.LENGTH_SHORT).show();
}
}
}
}
当然也可以采用github上整合好的包…
fragment中检查执行获取权限,要在fragment依附的activity中回调。
参考:
0.https://blog.csdn.net/LosingCarryJie/article/details/78408671
requestPermissions和checkSelfPermission源码解析
1.https://blog.csdn.net/ygz111111/article/details/80281966
android 6.0及以上动态权限的获取
2.https://blog.csdn.net/wangpf2011/article/details/80589648
Android6.0动态权限shouldShowRequestPermissionRationale的含义
3.https://blog.csdn.net/wuqingsen1/article/details/100142458
onRequestPermissionsResult回调不执行