android6.0以上权限问题
程序员文章站
2024-03-17 09:40:28
...
关于android6.0以上权限问题
方法一:使用AndroidAcp
Acp 为 Android check permission 缩写,此库简化Android 6.0 系统复杂的权限操作而编写。
1.关联
compile 'com.mylhyl:acp:1.1.7'
2.使用如下:
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE
, Manifest.permission.READ_PHONE_STATE
, Manifest.permission.SEND_SMS)
/*以下为自定义提示语、按钮文字
.setDeniedMessage()
.setDeniedCloseBtn()
.setDeniedSettingBtn()
.setRationalMessage()
.setRationalBtn()*/
.build(),
new AcpListener() {
@Override
public void onGranted() {
writeSD();
getIMEI();
}
@Override
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}
});
注意事项:
在android studio环境 请求Manifest.permission.CALL_PHONE权限时,Intent不能使用带参的构造函数(Intent.ACTION_CALL),否则会编译通不过的。 由于手机厂商深度定制系统的原因,很多预料不到滴,奇怪滴问题一大堆,如checkSelfPermission永远都是PERMISSION_DENIED或PERMISSION_GRANTED
方法二:注解MPermissions
基于Annotation Processor的简单易用的处理Android M运行时权限的库
1.关联
关联方式有两种
(1)在工程的build.gradle文件中添加
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
然后在module的build.gradle文件中
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
apt 'com.zhy:mpermission-compiler:1.0.0'
compile 'com.zhy:mpermission-api:1.0.0'
}
(2)直接在module.gradle文件中添加
annotationProcessor 'com.zhy:mpermission-compiler:1.0.0'
compile 'com.zhy:mpermission-api:1.0.0'
使用如下
public class MainActivity extends AppCompatActivity
{
private Button mBtnSdcard;
private static final int REQUECT_CODE_SDCARD = 2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnSdcard = (Button) findViewById(R.id.id_btn_sdcard);
mBtnSdcard.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
MPermissions.requestPermissions(MainActivity.this, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE,
READ_PHONE_STATE);//多个用逗号隔开
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@PermissionGrant(REQUECT_CODE_SDCARD)
public void requestSdcardSuccess()
{
Toast.makeText(this, "GRANT ACCESS SDCARD!", Toast.LENGTH_SHORT).show();
}
@PermissionDenied(REQUECT_CODE_SDCARD)
public void requestSdcardFailed()
{
Toast.makeText(this, "DENY ACCESS SDCARD!", Toast.LENGTH_SHORT).show();
}
}
3.混淆
-dontwarn com.zhy.m.**
-keep class com.zhy.m.** {*;}
-keep interface com.zhy.m.** { *; }
-keep class **$$PermissionProxy { *; }
上一篇: android6.0 ble 权限问题