普通权限和危险权限
1.危险权限(需要在manifiest里面申明,并且动态申请的权限)
<uses-permission android:name="android.permission.READ_CONTACTS"/> <!--读写联系人权限--> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!--有关电话的权限 比如打电话,获取电话信息等--> <uses-permission android:name="android.permission.READ_CALENDAR"/> <!--读,写日历权限--> <uses-permission android:name="android.permission.CAMERA"/> <!-- 相机权限 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!--位置权限,有获取粗糙和精准位置的权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <!--读,写存储区的权限 --> <uses-permission android:name="android.permission.BODY_SENSORS"/> <!-- 传感器权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <!--麦克风的自动记录的权限--> <uses-permission android:name="android.permission.READ_SMS"/> <!--读,写,发送短信等权限 -->
这9大类权限都是需要动态申请的。
2.普通权限(只需要在manifiest里面申请,不需要动态申请)
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
Android6.0动态申请涉及的方法
1.检查权限
/** * * @param permission 你需要被检查权限的名字 * * @return PackageManager.PERMISSION_GRANTED 代表已经拥有这个权限了 * PackageManager#PERMISSION_DENIED 代表没有拥有这个权限 */ public static int checkSelfPermission(@NonNull Context context, @NonNull String permission)
实际用法:
private boolean checkSelfPermission(){ boolean isRequestPermission = true; //代表所有权限是否已经被申请了 for(int i=0;i<NEED_PERMISSIONS.length;i++){ if(ContextCompat.checkSelfPermission(this,NEED_PERMISSIONS[i])!= PackageManager.PERMISSION_GRANTED){ requestPermissions.add(NEED_PERMISSIONS[i]); isRequestPermission = false; } } return isRequestPermission; }
2.申请权限:
ActivityCompat.requestPermisssion(Activity activity,String[] permissions.int requestCode) 该方法就可以动态申请权限,requestCode:用来判断返回结果时候的值
private void requestPermission(){
if(requestPermissions!=null&&requestPermissions.size()>0){
for (int i = 0; i < requestPermissions.size(); i++) {
ActivityCompat.requestPermissions
(this,requestPermissions.toArray(new String[requestPermissions.size()]),REQUEST_CODE);
}
}
}
3.权限的结果:重写onRequestPermissionsResult()方法
/**
*
* @param requestCode
* @param permissions 申请的权限
* @param grantResults 权限的结果的值.
* PackageManager.PERMISSION_GRANTED:权限申请成功
* PackageManager.PERMISSION_DENIED :权限申请失败
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case REQUEST_CODE:
for (int i = 0; i < grantResults.length; i++) {
if(grantResults[i]==PackageManager.PERMISSION_DENIED){
finish();
}
}
break;
}
}
4.解释权限:
应用场景:
1.用户第一次点击应用拒绝了权限
2.用户第二次点击应用,点击拒绝权限并且点击了不再询问的按钮
3.这时候用户在点开应用的时候,会直接拒绝了权限。这会导致用户体验所以android在这种情况下设置了解释权限的方法
方法的解释:
ActivityCompat.shouldShowRequestPermissionRationale(activity,permission);返回的结果值: 1.当用户拒绝过该权限并且的时候,返回
代码实现的场景,就在上面应用场景的时候,用户在点开应用我们可以弹出对话框,让用户去设置中打开权限
/** * * @param requestCode * @param permissions 申请的权限 * @param grantResults 权限的结果的值. * PackageManager.PERMISSION_GRANTED:权限申请成功 * PackageManager.PERMISSION_DENIED :权限申请失败 */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case REQUEST_CODE: boolean isRefuse = true ; //是否点击了不再提示的按钮 boolean isPass = true; for (int i = 0; i < permissions.length; i++){ if(ActivityCompat.shouldShowRequestPermissionRationale(this,permissions[i])){ isRefuse = false; } if(grantResults[i]==PackageManager.PERMISSION_DENIED) { isPass = false; } } if(!isPass&&isRefuse){ AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("权限缺失") .setMessage("需要前往设置中开启所需要的权限") .setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //前往该应用的设置中 Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.fromParts("package",getPackageName(),null)); try{ startActivity(intent); }catch (Exception e){ } finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }) .create(); dialog.show(); }else if(!isPass){ finish(); }else{ } break; } }