Android对6.0及以上的版本做了更强的权限控制。如打电话这种需要动态申请。我推荐使用github上start最多的PermissionsDispatcher作为权限管理。
说明:
一,使用的Androidstudio版本为3.2.1
二,使用的permissionsdispatcher版本为3.1.0
github地址为:https://github.com/permissions-dispatcher/PermissionsDispatcher
展示效果:
现在正式开始
1,在build.gradle中做如下代码1--4步骤所示配置。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mumu.jspermissions"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//1,增加jitpack支持
allprojects {
repositories {
maven { url "https://jitpack.io" }
maven { url "https://maven.google.com" }
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//2,butterKnife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//3,自己封装的弹窗
implementation 'com.github.mamumu:MMAlertDialog:10'
//4,permissionsdispatcher权限
implementation'com.github.hotchemi:permissionsdispatcher:3.1.0'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:3.1.0'
}
2,在AndroidManifest.xml中添加对应的权限,我demo中只是用拨打电话的权限,如果需要其他权限,都要先在这添加上。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mumu.jspermissions">
//拨打电话的权限
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3,前面1和2步骤完成了基本配置,核心6步来了。详情看下图和代码中注释。注意,被权限注解的方法不能用private修饰。
package com.mumu.jspermissions;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;
import com.mumu.alertdialog.MMAlertDialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;
/**
* @author : zlf
* date : 2019/1/22
* blog :https://www.jianshu.com/u/281e9668a5a6
* 1,在需要动态申请权限的类上添加 @RuntimePermissions 的注解
*/
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn1)
Button btn1;
//是否已经授权
private boolean isAllowPermissions = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn1)
public void onViewClicked() {
showDialog2();
}
/**
* 拨打电话的弹窗
*/
private void showDialog2() {
MMAlertDialogUtils.showDialog(this,
"客服电话",
"10086",
"取消",
"呼叫",
false,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//拨号方法
if (!isAllowPermissions) {
MainActivityPermissionsDispatcher.callPhoneWithPermissionCheck(MainActivity.this);
} else {
callPhone();
}
dialog.dismiss();
}
});
}
/**
* 2,拨打电话需要CALL_PHONE权限,在对应的方法是标明
*/
@NeedsPermission(Manifest.permission.CALL_PHONE)
void callPhone() {
isAllowPermissions = true;
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:10086");
intent.setData(data);
startActivity(intent);
}
/**
*,3,对需要该权限的解释
*/
@OnShowRationale(Manifest.permission.CALL_PHONE)
void showWhy(final PermissionRequest request) {
MMAlertDialogUtils.showDialog(this,
"提示",
"我们需要拨打电话权限,否则不能拨打电话",
"取消",
"授权",
false,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.cancel();
dialog.dismiss();
}
},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();
dialog.dismiss();
}
});
}
/**
*,4,当用户拒绝获取权限的提示
*/
@OnPermissionDenied(Manifest.permission.CALL_PHONE)
void showDenied() {
Toast.makeText(MainActivity.this, "无法获得权限", Toast.LENGTH_SHORT).show();
}
/**
*,5,当用户勾选不再提示并且拒绝的时候调用的方法
*/
@OnNeverAskAgain(Manifest.permission.CALL_PHONE)
void showNeverAskAgain() {
MMAlertDialogUtils.showDialog(MainActivity.this,
"权限设置提示",
"应用权限被拒绝,为了不影响您的正常使用,请在 权限 中开启对应权限",
"取消",
"进入设置",
false,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//引导用户至设置页手动授权
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
dialog.dismiss();
}
});
}
/**
*,6,权限回调,调用PermissionsDispatcher的回调方法
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
MainActivityPermissionsDispatcher.onRequestPermissionsResult(MainActivity.this, requestCode, grantResults);
}
}
4,MainActivityPermissionsDispatcher类需要如下图操作才可以对应生成,规则是当前类名+PermissionsDispatcher。
5,对应github地址
demo地址:https://github.com/mamumu/jsPermissions
如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。如果觉得对你有帮助欢迎给小星星,谢谢。