Android 6.0权限封装
相信大家都是Android的老油条了,都已经了解了Android 6.0以上的危险权限需要主动申请,6.0以下的危险权限只需要在AndroidManifest.xml文件配置下就可以了。每次配置和申请权限的过程中会感觉到代码重复量过多,比较烦躁,其实我也是,这样的话就就需要我们进行封装处理。那么如果我们在baseactivity中进行封装,然后每次只需要调用一个方法就进行了权限的申请是不是很方便呢?https://github.com/zhiyongshuangquan/permissions 点击下载demo。
接下来就直接看代码吧,还等什么?直接在base里面进行封装
package permissionsxfc.xfc.com.permissionsxfc;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xfc on 2018/5/3.
*/
public class BasePermissionActivity extends AppCompatActivity {
private final String TAG = "MPermissions";
private int REQUEST_CODE_PERMISSION = 0x00099;
/**
* 请求权限
*
* @param permissions 请求的权限
* @param requestCode 请求权限的请求码
*/
public void requestPermission(String[] permissions, int requestCode) {
this.REQUEST_CODE_PERMISSION = requestCode;
if (checkPermissions(permissions)) {
permissionSuccess(REQUEST_CODE_PERMISSION);
} else {
List<String> needPermissions = getDeniedPermissions(permissions);
ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
}
}
/**
* 检测所有的权限是否都已授权
*
* @param permissions
* @return
*/
private boolean checkPermissions(String[] permissions) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 获取权限集中需要申请权限的列表
*
* @param permissions
* @return
*/
private List<String> getDeniedPermissions(String[] permissions) {
List<String> needRequestPermissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED ||
ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
needRequestPermissionList.add(permission);
}
}
return needRequestPermissionList;
}
/**
* 系统请求权限回调
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (verifyPermissions(grantResults)) {
permissionSuccess(REQUEST_CODE_PERMISSION);
} else {
permissionFail(REQUEST_CODE_PERMISSION);
showTipsDialog();
}
}
}
/**
* 确认所有的权限是否都已授权
*
* @param grantResults
* @return
*/
private boolean verifyPermissions(int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 显示提示对话框
*/
private void showTipsDialog() {
new AlertDialog.Builder(this)
.setTitle("提示信息")
.setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
}).show();
}
/**
* 启动当前应用设置页面
*/
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
/**
* 获取权限成功
*
* @param requestCode
*/
public void permissionSuccess(int requestCode) {
Log.d(TAG, "获取权限成功=" + requestCode);
}
/**
* 权限获取失败
*
* @param requestCode
*/
public void permissionFail(int requestCode) {
Log.d(TAG, "获取权限失败=" + requestCode);
}
}
接下来就是如何使用了,代码继续
package permissionsxfc.xfc.com.permissionsxfc;
import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends BasePermissionActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 写SD卡
*
* @param view
*/
public void getSdperssion(View view) {
requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
@Override
public void permissionSuccess(int requestCode) {
super.permissionSuccess(requestCode);
switch (requestCode) {
case 0:
Toast.makeText(MainActivity.this, "SD权限获取成功", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void permissionFail(int requestCode) {
super.permissionFail(requestCode);
switch (requestCode) {
case 0:
Toast.makeText(MainActivity.this, "SD权限获取失败", Toast.LENGTH_LONG).show();
break;
}
}
}
是不是很方便,世界如此简单,就是这么随意,欢迎大家一起探讨,当然由于Android碎片化,5.0到6.0之间部分定制系统也进行了系统管理,需要进行特别处理,请听下次讲解。