当前手机各种系统权限比较多,很多时候需要动态的获取一些系统权限,添加其中,为此在写很多APP的时候需要在APP首个界面检查添加权限,所以自己封装了一个根据类。
PermissionUtils类
package com.fly.fengtaobao.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
/**
* <pre>
* .----.
* _.'__ `.
* .--(Q)(OK)---/$\
* .' @ /$$$\
* : , $$$$$
* `-..__.-' _.-\$$$/
* `;_: `"'
* .'"""""`.
* /, FLY ,\
* // \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* </pre>
* 包 名 : com.fly.fengtaobao.utils
* 作 者 : FLY
* 创建时间 : 2017/8/3
* <p>
* 描述: 动态获取权限
*/
public class PermissionUtils {
public static final int SETTING_REQUEST_CODE = 1111;
/**
* 获取请求权限中需要授权的权限
* @param context
* @param permissions 一个或多个权限
* @return
*/
public static List<String> getDeniedPermissions(Context context,String... permissions){
List<String> deniedPermissions = new ArrayList<String>();
for (String permission : permissions) {
//判断权限是否授予
if (ContextCompat.checkSelfPermission(context,permission)== PackageManager.PERMISSION_DENIED){
deniedPermissions.add(permission);
}
}
return deniedPermissions;
}
/**
* 授权权限
* @param activity
* @param permission_request_code
* @param permissions
* @return
*/
@TargetApi(Build.VERSION_CODES.M)
public static boolean isPermissionsGranted(Activity activity,int permission_request_code,String... permissions){
List<String> deniedPermissions = PermissionUtils.getDeniedPermissions(activity,permissions);
if (deniedPermissions != null && deniedPermissions.size()>0){
activity.requestPermissions(
deniedPermissions.toArray(new String[deniedPermissions.size()])
,permission_request_code);
//授权Activity回调
}else {
//权限通过
return true;
}
return false;
}
/**
* 验证所有权限是否都已经授权
* @param grantResults
* @return
*/
public static boolean verifyPermissions(int[] grantResults){
for (int grantResult:grantResults) {
if (grantResult != PackageManager.PERMISSION_DENIED){
return false;
}
}
return true;
}
/**
* 显示提示对话框
* @param context
*/
public static void showTipsDialog(Activity context){
LibDialogUtils.showPermissionTipsDialog(context);
}
public static void startAppSettings(Activity context){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:"+context.getPackageName()));
//得到新打开Activity关闭后返回的数据
//第二个参数为请求码,可以根据业务需求自己编号
context.startActivityForResult(intent,SETTING_REQUEST_CODE);
context.startActivity(intent);
}
}
LibDialogUtils类
package com.fly.fengtaobao.utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.fly.fengtaobao.R;
/**
* <pre>
* .----.
* _.'__ `.
* .--(Q)(OK)---/$\
* .' @ /$$$\
* : , $$$$$
* `-..__.-' _.-\$$$/
* `;_: `"'
* .'"""""`.
* /, FLY ,\
* // \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* </pre>
* 包 名 : com.fly.fengtaobao.utils
* 作 者 : FLY
* 创建时间 : 2017/8/4
* <p>
* 描述: Dialog工具类
*/
public class LibDialogUtils {
public static void showPermissionTipsDialog(final Activity activity){
new AlertDialog.Builder(activity)
.setTitle(R.string.LibDialogUtils_showPermissionTipsDialog_title)
.setMessage(R.string.LibDialogUtils_showPermissionTipsDialog_message)
.setPositiveButton(R.string.LibDialogUtils_showPermissionTipsDialog_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PermissionUtils.startAppSettings(activity);
}
}).show();
}
}
SplashActivity使用地方
package com.fly.fengtaobao.View;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.WindowManager;
import com.fly.fengtaobao.R;
import com.fly.fengtaobao.View.base.BaseActivity;
import com.fly.fengtaobao.View.common.config.Config;
import com.fly.fengtaobao.utils.PermissionUtils;
/**
* <pre>
* .----.
* _.'__ `.
* .--(Q)(OK)---/$\
* .' @ /$$$\
* : , $$$$$
* `-..__.-' _.-\$$$/
* `;_: `"'
* .'"""""`.
* /, FLY ,\
* // \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* </pre>
* 创建时间 : 2017/7/26
* <p>
* 描述: View --> 闪屏
* 1.动态权限的获取
* 2.判断是否可以正常获取到 手机串号
*/
public class SplashActivity extends BaseActivity {
/**
* 要动态获取权限时,延时100毫秒,进入下一个页面
*/
private static final long DELAY_MILLIS_PERMISSION = 100;
/**
* 正常情况下,延时1300毫秒,进入下一个页面
*/
private static final long DELAY_MILLIS = 500;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
/**
* 获取权限
*/
takePermission();
}
private void init() {
}
private void takePermission() {
if (PermissionUtils.isPermissionsGranted(this, Config.SPLASH_ACTIVITY_PERMISSION_REQUEST_CODE
, Config.mPermissionsSplashActivity)) {
onPermissionsGranted();
}
}
private void onPermissionsGranted() {
init();
}
private void onPermissionsDenied() {
PermissionUtils.showTipsDialog(this);
}
/**
* 申请权限的回调
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Config.SPLASH_ACTIVITY_PERMISSION_REQUEST_CODE) {
if (PermissionUtils.verifyPermissions(grantResults)) {
onPermissionsGranted();
} else {
onPermissionsDenied();
}
}
}
/**
* 从设置界面回来之后的回调,重新验证权限是否获取到
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PermissionUtils.SETTING_REQUEST_CODE) {
takePermission();
}
}
}
网友评论