1.Android Studio下载权限插件PermissionsDispatcher:
2.在app下面的:build.gradle的dependencies配红色的部分和android配红色部分
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.github.hotchemi:permissionsdispatcher:2.1.3' apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.3'//权限问题 }
android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "screen.hanwei.com" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } apply plugin: 'android-apt' }
3.在工程下面的:build.gradle同样配红色的部分
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
4.以上就完成了第三方6.0权限问题,下面在代码中添加需要的权限,在需要添加权限的地方鼠标点击右键会出现1中我们添加的插件
5.点击插件后会出现让你选择需要的权限,这个根据自己需要,我以内存读写为例;下面有四个方法,我就写两个
6.点击Generate生成,并且要在你需要的地方调用一下,我就在OnCreate中调用:
@NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}) void Screenshot() { } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); } @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}) void ScreenshotWrite(final PermissionRequest request) { }
在onCreate中调用下面这句话就ok了:
MainActivityPermissionsDispatcher.ScreenshotWithCheck(this);