關於MIUI懸浮窗權限問題的解決方案

先扯會....很久沒寫Blog了....這段時間有點小忙...瞎忙.....忙的本身都感受都不該該.....嚴重影響了生活質量......生活的幸福指數!!!.....到如今還特麼的單身!!!求介紹啊......html

MIUI是我我的很是喜歡的ROM....雖然有諸多的不爽....可是搞開發就能理解.....寫好一個產品是多麼的不易.....好多東西的不可控....精力的有限! 如今買手機都看能不能刷MIUI..不能刷的就猶豫了.....app

===============盜鏈....盜內容的都是Erbility, Shability  ========================ui

===============http://www.cnblogs.com/fangyucun/p/4027750.html=================spa

步入正題.....解決這個問題...無非就是2點線程

1.跳轉code

2.判斷是否真的打開component

......代碼打開是別想了....要否則人家也不能弄着玩啊.....除非是你發現了什麼....htm

跳轉的思路很簡單..手動找到那個界面..看看是哪一個Activity.blog

   public static ComponentName getTopComponentName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
        ComponentName componentName = taskInfo.get(0).topActivity;
        return componentName;
    }

再經過ComponentName想知道什麼不行啊.....不知道怎麼執行....能想到這個問題...不該該吧....隨便建議一下....跑個線程開發

下面就是跳轉了....

  /**
     * 打開MIUI權限管理界面(MIUI v5, v6)
     * @param context
     */
    public static void openMiuiPermissionActivity(Context context) {
        Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
        String rom = RomUtils.getRom();
        
        if (RomUtils.ROM_MIUI_V5.equals(rom)) {
            PackageInfo pInfo = null;
            try {
                pInfo = context.getPackageManager().getPackageInfo(packageName, 0);
            } catch (NameNotFoundException e) {
                Flog.e(e);
            }
            intent.setClassName(SETTINGS_PACKAGE_NAME, "com.miui.securitycenter.permission.AppPermissionsEditor");
            intent.putExtra("extra_package_uid", pInfo.applicationInfo.uid);
            
        } else if (RomUtils.ROM_MIUI_V6.equals(rom)) {
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
            intent.putExtra("extra_pkgname", context.getPackageName());
        }
        
        if (isIntentAvailable(context, intent)) {
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivityForResult(intent, 2);
            }
        } else {
            Flog.e("Intent is not available!");
        }
    }

很惋惜....V5的懸浮窗權限在應用詳情裏面...

  @TargetApi(9)
    public static void openAppDetailActivity(Context context, String packageName) {
        Intent intent = null;
        if (Build.VERSION.SDK_INT >= 9) {
            intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts(SCHEME_PACKAGE, packageName, null);
            intent.setData(uri);
        } else {
            final String className = Build.VERSION.SDK_INT == 8 ? 
                    SETTINGS_APPDETAILS_CLASS_NAME_22 : SETTINGS_APPDETAILS_CLASS_NAME_B21;
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setClassName(SETTINGS_PACKAGE_NAME, SETTINGS_APPDETAILS_CLASS_NAME);
            intent.putExtra(className, packageName);
        }
        if (isIntentAvailable(context, intent)) {
            context.startActivity(intent);
        } else {
            Flog.e("intent is not available!");
        }
    }

另外加了個Intent的判斷

  /**
     * 判斷是否有能夠接受的Activity
     * @param context
     * @param action
     * @return
     */
    public static boolean isIntentAvailable(Context context, Intent intent) {
        if (intent == null) return false;
        return context.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES).size() > 0;
    }

v5, v6不知道怎麼判斷? 好吧...有點跑題了....

    public static String getSystemProperty() {
        String line = null;
        BufferedReader reader = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop ro.miui.ui.version.name" );
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = reader.readLine();
            return line;
        } catch (IOException e) {
            Flog.e(e);
        } finally {
            IoUtils.close(reader);
        }
        return "UNKNOWN";
    }    

根據返回的是V5仍是V6判斷

=====完成跳轉.....下面就是判斷了......本身是沒琢磨出來....問的MIUI的工程師.....小帥哥太帥了.....感謝.....

    /**
     * 判斷MIUI的懸浮窗權限
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static boolean isMiuiFloatWindowOpAllowed(Context context) {
        final int version = Build.VERSION.SDK_INT;
        
        if (version >= 19) {
            return checkOp(context, OP_SYSTEM_ALERT_WINDOW);  //本身寫就是24 爲何是24?看AppOpsManager
        } else {
            if ((context.getApplicationInfo().flags & 1<<27) == 1) {
                return true;
            } else {
                return false;
            }
        }
    }  
    @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 {
                if (AppOpsManager.MODE_ALLOWED == (Integer)ReflectUtils.invokeMethod(manager, "checkOp", op, 
                    Binder.getCallingUid(), context.packageName())) { //這兒反射就本身寫吧
return true; } else { return false; } } catch (Exception e) { Flog.e(e.getMessage()); } } else { Flog.e("Below API 19 cannot invoke!"); } return false; }

到這兒就完事了...沒想到牽扯的代碼還挺多.......

相關文章
相關標籤/搜索