Android 6.0 权限申请

1. 以前的权限申请(sdk<23)

直接在AndroidManifest.xml中申明即可:

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

2. 运行时权限申请

void checkPermission() {
final List<String> permissionsList = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED))
permissionsList.add(Manifest.permission.READ_PHONE_STATE);
if ((checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED))
permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionsList.size() != 0) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
100);
}else {
//已经不是第一次,已经有权限
Log.e("test","permissionsList.size()==0");
}
}
}

这个checkPermission()Activity的方法.

3. 回调

private int count=0;
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
count++;
Log.e("test","onRequestPermissionsResult:"+count);
if (requestCode==100){
if (grantResults.length>0 && (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) && (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
}else { }
}
}

在logcat中:

08-03 20:09:20.545 16735-16735/didikee.com.demoapk E/test: onRequestPermissionsResult:1

当我们再次运行程序时,logcat中日志:

08-03 20:10:14.165 16735-16735/didikee.com.demoapk E/test: permissionsList.size()==0
04-28 19:08