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


先扯會....好久沒寫Blog了....這段時間有點小忙...瞎忙.....忙的自己都感覺都不應該.....嚴重影響了生活質量......生活的幸福指數!!!.....到現在還特么的單身!!!求介紹啊......

MIUI是我個人非常喜歡的ROM....雖然有諸多的不爽....但是搞開發就能理解.....寫好一個產品是多么的不易.....好多東西的不可控....精力的有限! 現在買手機都看能不能刷MIUI..不能刷的就猶豫了.....

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

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

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

1.跳轉

2.判斷是否真的打開

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

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

   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; }

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM