小米手机和魅族手机会默认关闭悬浮框,可以通过以下方法开启
package com.sinolbs.ecd.utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.os.Binder;
import android.os.Build;
/**
*
*
* MIUI 悬浮窗判断工具类
*/
public class AlterWindowUtil { public static final String TAG ="AlterWindowUtil"; /** * 4.4 以上可以直接判断准确 * * 4.4 以下非MIUI直接返回true * * 4.4 以下MIUI 可 判断 上一次打开app 时 是否开启了悬浮窗权限 * * @param context * @return */ @TargetApi(Build.VERSION_CODES.KITKAT) public static boolean isFloatWindowOpAllowed(Context context) { final int version = Build.VERSION.SDK_INT; if(!DeviceUtils.isFlyme4() && !DeviceUtils.isMiui()){ return true; } if (version >= 19) { return checkOp(context, 24); //自己写就是24 为什么是24?看AppOpsManager //AppOpsManager.OP_SYSTEM_ALERT_WINDOW } else { if(DeviceUtils.isMiui()){ if ((context.getApplicationInfo().flags & 1 << 27) == 1 <<27 ) { return true; } else { return false; } }else{ return true; } } } @TargetApi(Build.VERSION_CODES.KITKAT) public static boolean checkOp(Context context, int op) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); try { Class managerClass = manager.getClass(); Method method = managerClass.getDeclaredMethod("checkOp", int.class, int.class, String.class); int isAllowNum = (Integer) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName()); if (AppOpsManager.MODE_ALLOWED == isAllowNum) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); } } return false; } }
if (!AlterWindowUtil.isFloatWindowOpAllowed(getActivity())) {ToastUtil.showMessage("请打开悬浮框权限");miui(arg0);}
public void miui(View v) {try {Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");localIntent.setClassName("com.miui.securitycenter","com.miui.permcenter.permissions.AppPermissionsEditorActivity");localIntent.putExtra("extra_pkgname", v.getContext().getPackageName());v.getContext().startActivity(localIntent);} catch (ActivityNotFoundException localActivityNotFoundException) {Intent intent1 = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);Uri uri = Uri.fromParts("package", v.getContext().getPackageName(),null);intent1.setData(uri);v.getContext().startActivity(intent1);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!