mainfest.xml先声明你需要得到的权限。
使用方式:
package com.example.huangrenqian833.myapplication;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.example.huangrenqian833.myapplication.permission.GetPermission;
import com.example.huangrenqian833.myapplication.permission.module.PermissionResultListener;
/**
* Created by huangrenqian833 on 2018/12/24.
*/
public class TestPermissionActivity extends AppCompatActivity implements PermissionResultListener {
public static final int PERMISSION_REQUE_CAMERA_AND_RECORD_CODE = 116;
public static final int PERMISSION_REQUE_CAMERA_CODE = 115;
public static final int PERMISSION_REQUE_PHOTO_VIDEO = 114;
public static final int PERMISSION_LOCATION_CODE = 118;
private static final String[] PERMISSIONS_LOCATION = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
private static final int PERMISSION_OPEN_SDK = 119;
private static final int PERMISSION_OPEN_READ_SDK = 120;
private static final int PERMISSION_SELECT_FILE = 121;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.email_sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetPermission.with(TestPermissionActivity.this)
.permissions(PERMISSIONS_LOCATION)
.requestCode(PERMISSION_LOCATION_CODE)
.listener(TestPermissionActivity.this)
.request();
}
});
findViewById(R.id.email_sign_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetPermission.with(TestPermissionActivity.this)
.permissions(Manifest.permission.CAMERA)
.requestCode(PERMISSION_REQUE_CAMERA_CODE)
.listener(TestPermissionActivity.this)
.request();
}
});
findViewById(R.id.email_sign_out_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetPermission.with(TestPermissionActivity.this)
.permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.requestCode(PERMISSION_SELECT_FILE)
.listener(TestPermissionActivity.this)
.request();
}
});
}
/**
* 建议在BaseActivity/BaseFragment加这个方法
*
* @param requestCode requestCode
* @param permissions permissions
* @param grantResults grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
GetPermission.onRequestPermissionResult(this, requestCode, permissions, grantResults);
}
/**
* 建议在BaseActivity/BaseFragment加这个方法
*
* @param requestCode requestCode
* @param resultCode resultCode
* @param data data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
GetPermission.onActivityResult(this, requestCode, resultCode, data);
}
@Override
public void onPermissionSuccess(int requestCode) {
if (PERMISSION_REQUE_CAMERA_AND_RECORD_CODE == requestCode) {//相机与录音
Log.e("TestPermissionActivity", "获得相机与录音权限");
} else if (PERMISSION_REQUE_CAMERA_CODE == requestCode) {//相机
Log.e("TestPermissionActivity", "获得相机权限");
} else if (PERMISSION_REQUE_PHOTO_VIDEO == requestCode) {//录制视频
Log.e("TestPermissionActivity", "获得录制视频权限");
} else if (PERMISSION_OPEN_SDK == requestCode) {//选择视频
Log.e("TestPermissionActivity", "获得选择视频权限");
} else if (PERMISSION_SELECT_FILE == requestCode) {//选择文件
Log.e("TestPermissionActivity", "获得选择文件权限");
} else if (PERMISSION_LOCATION_CODE == requestCode) {//定位
Log.e("TestPermissionActivity", "获得定位权限");
}
}
@Override
public void onPermissionShouldShow(int requestCode) {
if (PERMISSION_REQUE_CAMERA_AND_RECORD_CODE == requestCode) {//相机与录音
Log.e("TestPermissionActivity", "拒绝将无法获得相机与录音权限");
} else if (PERMISSION_REQUE_CAMERA_CODE == requestCode) {//相机
Log.e("TestPermissionActivity", "拒绝将无法获得相机权限");
} else if (PERMISSION_REQUE_PHOTO_VIDEO == requestCode) {//录制视频
Log.e("TestPermissionActivity", "拒绝将无法获得录制视频权限");
} else if (PERMISSION_OPEN_SDK == requestCode) {//选择视频
Log.e("TestPermissionActivity", "拒绝将无法获得选择视频权限");
} else if (PERMISSION_SELECT_FILE == requestCode) {//选择文件
Log.e("TestPermissionActivity", "拒绝将无法获得选择文件权限");
} else if (PERMISSION_LOCATION_CODE == requestCode) {//定位
Log.e("TestPermissionActivity", "拒绝将无法获得定位权限");
}
}
@Override
public void onPermissionFail(int requestCode) {
if (PERMISSION_REQUE_CAMERA_AND_RECORD_CODE == requestCode) {//相机与录音
Log.e("TestPermissionActivity", "获得相机与录音权限失败");
} else if (PERMISSION_REQUE_CAMERA_CODE == requestCode) {//相机
Log.e("TestPermissionActivity", "获得相机权限失败");
} else if (PERMISSION_REQUE_PHOTO_VIDEO == requestCode) {//录制视频
Log.e("TestPermissionActivity", "获得录制视频权限失败");
} else if (PERMISSION_OPEN_SDK == requestCode) {//选择视频
Log.e("TestPermissionActivity", "获得选择视频权限失败");
} else if (PERMISSION_SELECT_FILE == requestCode) {//选择文件
Log.e("TestPermissionActivity", "获得选择文件权限失败");
} else if (PERMISSION_LOCATION_CODE == requestCode) {//定位
Log.e("TestPermissionActivity", "获得定位权限失败");
}
}
}
代码结构:
com.example.huangrenqian833.myapplication
.annotation
.builder
.module
.uitls
package com.example.huangrenqian833.myapplication.permission.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 请求权限失败,勾选了不再提醒,无法再次拉起系统弹框,需要自行处理结果
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PermissionFail {
int requestCode();
}
package com.example.huangrenqian833.myapplication.permission.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 请求权限成功
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PermissionSuccess {
int requestCode();
}
package com.example.huangrenqian833.myapplication.permission.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 用来注解未勾选不再提醒,下次还可以请求权限系统弹框
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PermissionShouldShow {
int requestCode();
}
package com.example.huangrenqian833.myapplication.permission.builder;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
/**
* ActivityPermissionBuilder
*/
public class ActivityPermissionBuilder extends BasePermissionBuilder {
public ActivityPermissionBuilder(Activity object) {
super(object);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void requestPermission(int requestCode, String[] permissions) {
object.requestPermissions(permissions, requestCode);
}
@Override
protected Activity getActivity() {
return object;
}
}
package com.example.huangrenqian833.myapplication.permission.builder;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import com.example.huangrenqian833.myapplication.R;
import com.example.huangrenqian833.myapplication.permission.module.PermissionListenerManager;
import com.example.huangrenqian833.myapplication.permission.module.PermissionResultListener;
import com.example.huangrenqian833.myapplication.permission.uitls.AnnotationUtil;
import com.example.huangrenqian833.myapplication.permission.uitls.SPUtil;
import java.util.List;
/**
* BasePermissionBuilder
*/
public abstract class BasePermissionBuilder {
private String[] permissions;
private int requestCode;
protected T object;
private PermissionResultListener listener;
public BasePermissionBuilder(T object) {
this.object = object;
}
public BasePermissionBuilder permissions(String... permissions) {
this.permissions = permissions;
return this;
}
public BasePermissionBuilder requestCode(int requestCode) {
this.requestCode = requestCode;
return this;
}
public BasePermissionBuilder listener(PermissionResultListener listener) {
this.listener = listener;
return this;
}
public void request() {
if (permissions == null || permissions.length == 0) {
Log.e("BasePermissionBuilder", "permissions is empty !!!");
return;
}
if (object == null) {
Log.e("BasePermissionBuilder", "object is null !!!");
return;
}
if (!AnnotationUtil.isOverMarshmallow()) {
doPermissionSuccess();
return;
}
//如果权限组中有悬浮框权限,则需要走悬浮框权限逻辑,否则参考正常逻辑处理
String systemAlertWindow = AnnotationUtil.getSystemAlertWindow(permissions);
if (!TextUtils.isEmpty(systemAlertWindow)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(getActivity())) {
doPermissionSuccess();
} else {
openDrawOverlaysDialog(systemAlertWindow);
}
}
String[] newPermission = AnnotationUtil.getNewPermission(permissions, systemAlertWindow);
if (newPermission == null || newPermission.length == 0) {
return;
}
requestPermission(newPermission);
} else {
requestPermission(permissions);
}
}
private void openDrawOverlaysDialog(final String permission) {
boolean isShowing = SPUtil.getBoolean(getActivity(), SPUtil.SP_NAME_PERMISSION, SPUtil.SP_KEY_PERMISSION_DRAW_OVERLAYS, false);
if (!isShowing) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_draw_overlays, null);
CheckBox checkBox = view.findViewById(R.id.dialog_cb);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SPUtil.putBoolean(getActivity(), SPUtil.SP_NAME_PERMISSION, SPUtil.SP_KEY_PERMISSION_DRAW_OVERLAYS, isChecked);
}
});
new AlertDialog.Builder(getActivity())
.setTitle("提示")
.setView(view)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PermissionListenerManager.getInstance().addListener(requestCode, listener);
PermissionListenerManager.getInstance().addPermission(requestCode, permission);
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getActivity().getPackageName()));
getActivity().startActivityForResult(intent, requestCode);
}
}).create()
.show();
} else {
doPermissionFail();
}
}
/**
* 请求权限
*
* @param permissions
*/
private void requestPermission(String[] permissions) {
List deniedPermissions = AnnotationUtil.findDeniedPermissions(getActivity(), permissions);
if (deniedPermissions != null && deniedPermissions.size() > 0) {
PermissionListenerManager.getInstance().addListener(requestCode, listener);
requestPermission(requestCode, deniedPermissions.toArray(new String[deniedPermissions.size()]));
} else {
doPermissionSuccess();
}
}
private void doPermissionSuccess() {
if (listener != null) {
listener.onPermissionSuccess(requestCode);
} else {
AnnotationUtil.doExecuteSuccess(object, requestCode);
}
}
private void doPermissionFail() {
if (listener != null) {
listener.onPermissionFail(requestCode);
} else {
AnnotationUtil.doExecuteFail(object, requestCode);
}
}
@TargetApi(Build.VERSION_CODES.M)
protected abstract void requestPermission(int requestCode, String[] permissions);
protected abstract Activity getActivity();
}
package com.example.huangrenqian833.myapplication.permission.builder;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.os.Build;
/**
* FragmentPermissionBuilder
*/
@Deprecated
public class FragmentPermissionBuilder extends BasePermissionBuilder{
public FragmentPermissionBuilder(Fragment object) {
super(object);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void requestPermission(int requestCode, String[] permissions) {
object.requestPermissions(permissions, requestCode);
}
@Override
protected Activity getActivity() {
return object.getActivity();
}
}
package com.example.huangrenqian833.myapplication.permission.builder;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.support.v4.app.Fragment;
/**
* FragmentV4PermissionBuilder
*/
public class FragmentV4PermissionBuilder extends BasePermissionBuilder {
public FragmentV4PermissionBuilder(Fragment object) {
super(object);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void requestPermission(int requestCode, String[] permissions) {
object.requestPermissions(permissions, requestCode);
}
@Override
protected Activity getActivity() {
return object.getActivity();
}
}
package com.example.huangrenqian833.myapplication.permission.module;
import android.text.TextUtils;
import java.lang.ref.WeakReference;
import java.util.HashMap;
/**
* PermissionListenerManager
*/
public class PermissionListenerManager {
private static PermissionListenerManager instance;
private volatile HashMap> listeners = new HashMap<>();
private volatile HashMap permissions = new HashMap<>();
public static PermissionListenerManager getInstance() {
if (instance == null) {
synchronized (PermissionListenerManager.class) {
if (instance == null) {
instance = new PermissionListenerManager();
}
}
}
return instance;
}
public synchronized PermissionResultListener getListener(int requestCode) {
WeakReference weakReference = listeners.get(requestCode);
if (weakReference != null) {
return weakReference.get();
}
return null;
}
/**
* 使用弱引用保存对象
*
* @param requestCode
* @param listener
*/
public synchronized void addListener(int requestCode, PermissionResultListener listener) {
if (listener != null) {
listeners.put(requestCode, new WeakReference<>(listener));
}
}
public synchronized void removeListener(int requestCode) {
listeners.remove(requestCode);
}
public synchronized void cleanListener() {
listeners.clear();
}
public synchronized String getPermission(int requestCode) {
return permissions.get(requestCode);
}
public synchronized void addPermission(int requestCode, String permission) {
if (!TextUtils.isEmpty(permission)) {
permissions.put(requestCode, permission);
}
}
public synchronized void removePermission(int requestCode) {
permissions.remove(requestCode);
}
public synchronized void cleanPermission() {
permissions.clear();
}
}
package com.example.huangrenqian833.myapplication.permission.module;
/**
* 权限结果监听
*/
public interface PermissionResultListener {
/**
* 授权成功
*
* @param requestCode
*/
void onPermissionSuccess(int requestCode);
/**
* 授权失败,未勾选不再提醒,还可以继续弹出系统权限框
*
* @param requestCode
*/
void onPermissionShouldShow(int requestCode);
/**
* 授权失败,勾选不再提醒,无法拉起系统权限框
*
* @param requestCode
*/
void onPermissionFail(int requestCode);
}
package com.example.huangrenqian833.myapplication.permission.uitls;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import com.example.huangrenqian833.myapplication.permission.annotation.PermissionFail;
import com.example.huangrenqian833.myapplication.permission.annotation.PermissionShouldShow;
import com.example.huangrenqian833.myapplication.permission.annotation.PermissionSuccess;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* AnnotationUtil
*/
public class AnnotationUtil {
public static boolean checkPermission(Context context, String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED;
}
return true;
}
public static boolean isOverMarshmallow() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
@TargetApi(value = Build.VERSION_CODES.M)
public static List findDeniedPermissions(Activity activity, String[] permissions) {
List denyPermissions = null;
if (permissions != null) {
denyPermissions = new ArrayList<>();
for (String p : permissions) {
if (checkPermission(activity, p)) {
denyPermissions.add(p);
}
}
}
return denyPermissions;
}
public static String getSystemAlertWindow(String[] permissions) {
if (permissions != null) {
for (String p : permissions) {
if (Manifest.permission.SYSTEM_ALERT_WINDOW.equals(p)) {
return p;
}
}
}
return null;
}
/**
* @param permissions 权限列表
* @param removePermissions 需要移除的权限
* @return
*/
public static String[] getNewPermission(String[] permissions, String... removePermissions) {
if (permissions != null && removePermissions != null) {
List ps = new ArrayList();
for (int i = 0; i < permissions.length; i++) {
ps.add(permissions[i]);
}
List rps = new ArrayList<>();
for (int i = 0; i < removePermissions.length; i++) {
rps.add(removePermissions[i]);
}
for (int i = ps.size(); i > 0; i--) {
String pss = ps.get(i - 1);
for (int j = rps.size(); j > 0; j--) {
String rpss = rps.get(j - 1);
if (pss.equals(rpss)) {
ps.remove(i - 1);
rps.remove(j - 1);
}
}
}
if (ps.size() > 0) {
permissions = ps.toArray(new String[ps.size()]);
} else {
permissions = null;
}
}
return permissions;
}
public static Method findMethodWithRequestCode(Class> aClass, Class annotation, int requestCode) {
for (Method method : aClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(annotation)) {
if (isEqualRequestCodeFromAnnotation(method, annotation, requestCode)) {
return method;
}
}
}
return null;
}
private static boolean isEqualRequestCodeFromAnnotation(Method method, Class annotation, int requestCode) {
if (annotation.equals(PermissionFail.class)) {
return requestCode == method.getAnnotation(PermissionFail.class).requestCode();
} else if (annotation.equals(PermissionShouldShow.class)) {
return requestCode == method.getAnnotation(PermissionShouldShow.class).requestCode();
} else if (annotation.equals(PermissionSuccess.class)) {
return requestCode == method.getAnnotation(PermissionSuccess.class).requestCode();
}
return false;
}
public static void doExecuteSuccess(Object object, int requestCode) {
Method executeMethod = AnnotationUtil.findMethodWithRequestCode(object.getClass(), PermissionSuccess.class, requestCode);
executeMethod(object, executeMethod);
}
public static void doExecuteShouldShow(Object object, int requestCode) {
Method executeMethod = AnnotationUtil.findMethodWithRequestCode(object.getClass(), PermissionShouldShow.class, requestCode);
executeMethod(object, executeMethod);
}
public static void doExecuteFail(Object object, int requestCode) {
Method executeMethod = AnnotationUtil.findMethodWithRequestCode(object.getClass(), PermissionFail.class, requestCode);
executeMethod(object, executeMethod);
}
private static void executeMethod(Object object, Method executeMethod) {
try {
if (executeMethod != null) {
if (!executeMethod.isAccessible()) {
executeMethod.setAccessible(true);
}
executeMethod.invoke(object);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.huangrenqian833.myapplication.permission.uitls;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
/**
* SPUtil
*/
public class SPUtil {
/**
* 权限状态sp文件
*/
public static final String SP_NAME_PERMISSION = "name_permission";
public static final String SP_KEY_PERMISSION_DRAW_OVERLAYS = "key_draw_overlays";
public static void putBoolean(@NonNull Context context, @NonNull String name, @NonNull String key, boolean bol) {
SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
sp.edit()
.putBoolean(key, bol)
.commit();
}
public static void getBoolean(@NonNull Context context, @NonNull String name, @NonNull String key) {
getBoolean(context, name, key, false);
}
public static boolean getBoolean(@NonNull Context context, @NonNull String name, @NonNull String key, boolean defBol) {
SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
return sp.getBoolean(key, defBol);
}
}
package com.example.huangrenqian833.myapplication.permission;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import com.example.huangrenqian833.myapplication.permission.builder.ActivityPermissionBuilder;
import com.example.huangrenqian833.myapplication.permission.builder.FragmentPermissionBuilder;
import com.example.huangrenqian833.myapplication.permission.builder.FragmentV4PermissionBuilder;
import com.example.huangrenqian833.myapplication.permission.module.PermissionListenerManager;
import com.example.huangrenqian833.myapplication.permission.module.PermissionResultListener;
import com.example.huangrenqian833.myapplication.permission.uitls.AnnotationUtil;
import com.example.huangrenqian833.myapplication.permission.uitls.SPUtil;
import java.util.ArrayList;
import java.util.List;
/**
* 权限入口
* 这里提供了两种实现方式,具体使用方法可见demo
* 方法一:注解方法的形式
* 方法二:监听回调的方式
*/
public class GetPermission {
public static ActivityPermissionBuilder with(Activity activity) {
return new ActivityPermissionBuilder(activity);
}
public static FragmentV4PermissionBuilder with(Fragment fragment) {
return new FragmentV4PermissionBuilder(fragment);
}
@Deprecated
public static FragmentPermissionBuilder with(android.app.Fragment fragment) {
return new FragmentPermissionBuilder(fragment);
}
public static void onRequestPermissionResult(Activity activity, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
onRequestResult(activity, requestCode, permissions, grantResults);
}
public static void onRequestPermissionResult(Fragment fragment, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
onRequestResult(fragment, requestCode, permissions, grantResults);
}
@Deprecated
public static void onRequestPermissionResult(android.app.Fragment fragment, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
onRequestResult(fragment, requestCode, permissions, grantResults);
}
/**
* 处理响应权限结果
*
* @param object
* @param requestCode
* @param permissions
* @param grantResults
*/
private static void onRequestResult(Object object, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
List deniedPermissions = new ArrayList<>();
if (grantResults != null) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
deniedPermissions.add(permissions[i]);
}
}
}
//获取到listener对象以后,从PermissionListenerManager中移除listener
PermissionResultListener listener = PermissionListenerManager.getInstance().getListener(requestCode);
if (listener != null) {
PermissionListenerManager.getInstance().removeListener(requestCode);
}
if (deniedPermissions.size() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Activity activity = getActivity(object);
boolean flag = true;
int size = deniedPermissions.size();
for (int i = 0; i < size; i++) {
flag = flag && activity.shouldShowRequestPermissionRationale(deniedPermissions.get(i));
}
if (flag) {
if (listener != null) {
listener.onPermissionShouldShow(requestCode);
} else {
AnnotationUtil.doExecuteShouldShow(object, requestCode);
}
} else {
if (listener != null) {
listener.onPermissionFail(requestCode);
} else {
AnnotationUtil.doExecuteFail(object, requestCode);
}
}
}
} else {
if (listener != null) {
listener.onPermissionSuccess(requestCode);
} else {
AnnotationUtil.doExecuteSuccess(object, requestCode);
}
}
}
private static Activity getActivity(Object object) {
if (object instanceof Activity) {
return (Activity) object;
} else if (object instanceof Fragment) {
return ((Fragment) object).getActivity();
} else if (object instanceof android.app.Fragment) {
return ((android.app.Fragment) object).getActivity();
}
throw new RuntimeException("object type is error !");
}
public static void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
onResult(activity, requestCode, resultCode, data);
}
public static void onActivityResult(Fragment activity, int requestCode, int resultCode, Intent data) {
onResult(activity, requestCode, resultCode, data);
}
public static void onActivityResult(android.app.Fragment activity, int requestCode, int resultCode, Intent data) {
onResult(activity, requestCode, resultCode, data);
}
private static void onResult(Object object, int requestCode, int resultCode, Intent data) {
String permission = PermissionListenerManager.getInstance().getPermission(requestCode);
if (!TextUtils.isEmpty(permission)) {
PermissionListenerManager.getInstance().removePermission(requestCode);
//悬浮窗口权限
if (Manifest.permission.SYSTEM_ALERT_WINDOW.equals(permission)) {
boolean isShowing = SPUtil.getBoolean(getActivity(object), SPUtil.SP_NAME_PERMISSION, SPUtil.SP_KEY_PERMISSION_DRAW_OVERLAYS, false);
PermissionResultListener listener = PermissionListenerManager.getInstance().getListener(requestCode);
if (listener != null) {
PermissionListenerManager.getInstance().removeListener(requestCode);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(getActivity(object))) {
listener.onPermissionSuccess(requestCode);
} else {
if (!isShowing) {
listener.onPermissionShouldShow(requestCode);
} else {
listener.onPermissionFail(requestCode);
}
}
} else {
listener.onPermissionSuccess(requestCode);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(getActivity(object))) {
AnnotationUtil.doExecuteSuccess(object, requestCode);
} else {
if (!isShowing) {
AnnotationUtil.doExecuteShouldShow(object, requestCode);
} else {
AnnotationUtil.doExecuteFail(object, requestCode);
}
}
} else {
AnnotationUtil.doExecuteSuccess(object, requestCode);
}
}
}
}
}
}
dialog_draw_overlays