Android中怎么實(shí)現(xiàn)懸浮窗權(quán)限,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)建站成都網(wǎng)站建設(shè)按需網(wǎng)站策劃,是成都網(wǎng)站設(shè)計(jì)公司,為資質(zhì)代辦提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開(kāi)發(fā)等。成都網(wǎng)站改版熱線:18982081108
懸浮窗適配
懸浮窗適配有兩種方法:第一種是按照正規(guī)的流程,如果系統(tǒng)沒(méi)有賦予 APP 彈出懸浮窗的權(quán)限,就先跳轉(zhuǎn)到權(quán)限授權(quán)界面,等用戶打開(kāi)該權(quán)限之后,再去彈出懸浮窗,比如 QQ 等一些主流應(yīng)用就是這么做得;第二種就是利用系統(tǒng)的漏洞,繞過(guò)權(quán)限的申請(qǐng),簡(jiǎn)單粗暴,這種方法我不是特別建議,但是現(xiàn)在貌似有些應(yīng)用就是這樣,比如 UC 和有道詞典,這樣適配在大多數(shù)手機(jī)上都是 OK 的,但是在一些特殊的機(jī)型不行,比如某米的 miui8。
正常適配流程
在 4.4~5.1.1 版本之間,和 6.0~最新版本之間的適配方法是不一樣的,之前的版本由于 google 并沒(méi)有對(duì)這個(gè)權(quán)限進(jìn)行單獨(dú)處理,所以是各家手機(jī)廠商根據(jù)需要定制的,所以每個(gè)權(quán)限的授權(quán)界面都各不一樣,適配起來(lái)難度較大,6.0 之后適配起來(lái)就相對(duì)簡(jiǎn)單很多了。
Android 4.4 ~ Android 5.1.1
由于判斷權(quán)限的類 AppOpsManager 是 API19 版本添加,所以Android 4.4 之前的版本(不包括4.4)就不用去判斷了,直接調(diào)用 WindowManager 的 addView 方法彈出即可,但是貌似有些特殊的手機(jī)廠商在 API19 版本之前就已經(jīng)自定義了懸浮窗權(quán)限,如果有發(fā)現(xiàn)的,請(qǐng)聯(lián)系我。
眾所周知,國(guó)產(chǎn)手機(jī)的種類實(shí)在是過(guò)于豐富,而且一個(gè)品牌的不同版本還有不一樣的適配方法,比如某米(嫌棄臉),所以我在實(shí)際適配的過(guò)程中總結(jié)了幾種通用的方法, 大家可以參考一下:
直接百度一下,搜索關(guān)鍵詞“小米手機(jī)懸浮窗適配”等;
看看 QQ 或者其他的大公司 APP 是否已經(jīng)適配,如果已經(jīng)適配,跳轉(zhuǎn)到相關(guān)權(quán)限授權(quán)頁(yè)面之后,或者自己能夠直接在設(shè)置里找到懸浮窗權(quán)限授權(quán)頁(yè)面也是一個(gè)道理,使用 adb shell dumpsys activity 命令,找到相關(guān)的信息,如下圖所示
可以清楚看到授權(quán) activity 頁(yè)面的包名和 activity 名,而且可以清楚地知道跳轉(zhuǎn)的 intent 是否帶了 extra,如果沒(méi)有 extra 就可以直接跳入,如果帶上了 extra,百度一下該 activity 的名字,看能否找到有用信息,比如適配方案或者源碼 APK 之類的;
依舊利用上面的方法,找到 activity 的名字,然后 root 準(zhǔn)備適配的手機(jī),直接在相關(guān)目錄 /system/app 下把源碼 APK 拷貝出來(lái),反編譯,根據(jù) activity 的名字找到相關(guān)代碼,之后的事情就簡(jiǎn)單了;
還有一個(gè)方法就是發(fā)動(dòng)人力資源去找,看看已經(jīng)適配該手機(jī)機(jī)型的 app 公司是否有自己認(rèn)識(shí)的人,或者干脆點(diǎn),直接找這個(gè)手機(jī)公司里面是否有自己認(rèn)識(shí)的手機(jī)開(kāi)發(fā)朋友,直接詢問(wèn),方便快捷。
常規(guī)手機(jī)
由于 6.0 之前的版本常規(guī)手機(jī)并沒(méi)有把懸浮窗權(quán)限單獨(dú)拿出來(lái),所以正常情況下是可以直接使用 WindowManager.addView 方法直接彈出懸浮窗。
如何判斷手機(jī)的機(jī)型,辦法很多,在這里我就不貼代碼了,一般情況下在 terminal 中執(zhí)行 getprop 命令,然后在打印出來(lái)的信息中找到相關(guān)的機(jī)型信息即可,這里貼出國(guó)產(chǎn)幾款常見(jiàn)機(jī)型的判斷:
/** * 獲取 emui 版本號(hào) * @return */ public static double getEmuiVersion() { try { String emuiVersion = getSystemProperty("ro.build.version.emui"); String version = emuiVersion.substring(emuiVersion.indexOf("_") + 1); return Double.parseDouble(version); } catch (Exception e) { e.printStackTrace(); } return 4.0; } /** * 獲取小米 rom 版本號(hào),獲取失敗返回 -1 * * @return miui rom version code, if fail , return -1 */ public static int getMiuiVersion() { String version = getSystemProperty("ro.miui.ui.version.name"); if (version != null) { try { return Integer.parseInt(version.substring(1)); } catch (Exception e) { Log.e(TAG, "get miui version code error, version : " + version); } } return -1; } public static String getSystemProperty(String propName) { String line; BufferedReader input = null; try { Process p = Runtime.getRuntime().exec("getprop " + propName); input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); line = input.readLine(); input.close(); } catch (IOException ex) { Log.e(TAG, "Unable to read sysprop " + propName, ex); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { Log.e(TAG, "Exception while closing InputStream", e); } } } return line; } public static boolean checkIsHuaweiRom() { return Build.MANUFACTURER.contains("HUAWEI"); } /** * check if is miui ROM */ public static boolean checkIsMiuiRom() { return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name")); } public static boolean checkIsMeizuRom() { //return Build.MANUFACTURER.contains("Meizu"); String meizuFlymeOSFlag = getSystemProperty("ro.build.display.id"); if (TextUtils.isEmpty(meizuFlymeOSFlag)){ return false; }else if (meizuFlymeOSFlag.contains("flyme") || meizuFlymeOSFlag.toLowerCase().contains("flyme")){ return true; }else { return false; } } /** * check if is 360 ROM */ public static boolean checkIs360Rom() { return Build.MANUFACTURER.contains("QiKU"); }
小米
首先需要適配的就應(yīng)該是小米了,而且比較麻煩的事情是,miui 的每個(gè)版本適配方法都是不一樣的,所以只能每個(gè)版本去單獨(dú)適配,不過(guò)還好由于使用的人數(shù)多,網(wǎng)上的資料也比較全。首先第一步當(dāng)然是判斷是否賦予了懸浮窗權(quán)限,這個(gè)時(shí)候就需要使用到 AppOpsManager 這個(gè)類了,它里面有一個(gè) checkop 方法:
/** * Do a quick check for whether an application might be able to perform an operation. * This is <em>not</em> a security check; you must use {@link #noteOp(int, int, String)} * or {@link #startOp(int, int, String)} for your actual security checks, which also * ensure that the given uid and package name are consistent. This function can just be * used for a quick check to see if an operation has been disabled for the application, * as an early reject of some work. This does not modify the time stamp or other data * about the operation. * @param op The operation to check. One of the OP_* constants. * @param uid The user id of the application attempting to perform the operation. * @param packageName The name of the application attempting to perform the operation. * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without * causing the app to crash). * @throws SecurityException If the app has been configured to crash on this op. * @hide */ public int checkOp(int op, int uid, String packageName) { try { int mode = mService.checkOperation(op, uid, packageName); if (mode == MODE_ERRORED) { throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName)); } return mode; } catch (RemoteException e) { } return MODE_IGNORED; }
找到懸浮窗權(quán)限的 op 值是:
/** @hide */ public static final int OP_SYSTEM_ALERT_WINDOW = 24;
注意到這個(gè)函數(shù)和這個(gè)值其實(shí)都是 hide 的,所以沒(méi)辦法,你懂的,只能用反射:
/** * 檢測(cè) miui 懸浮窗權(quán)限 */ public static boolean checkFloatWindowPermission(Context context) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24; } else { // if ((context.getApplicationInfo().flags & 1 << 27) == 1) { // return true; // } else { // return false; // } return true; } } @TargetApi(Build.VERSION_CODES.KITKAT) private 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 clazz = AppOpsManager.class; Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class); return AppOpsManager.MODE_ALLOWED == (int)method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName()); } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } } else { Log.e(TAG, "Below API 19 cannot invoke!"); } return false; }
檢測(cè)完成之后就是跳轉(zhuǎn)到授權(quán)頁(yè)面去開(kāi)啟權(quán)限了,但是由于 miui 不同版本的權(quán)限授權(quán)頁(yè)面不一樣,所以需要根據(jù)不同版本進(jìn)行不同處理:
/** * 獲取小米 rom 版本號(hào),獲取失敗返回 -1 * * @return miui rom version code, if fail , return -1 */ public static int getMiuiVersion() { String version = RomUtils.getSystemProperty("ro.miui.ui.version.name"); if (version != null) { try { return Integer.parseInt(version.substring(1)); } catch (Exception e) { Log.e(TAG, "get miui version code error, version : " + version); Log.e(TAG, Log.getStackTraceString(e)); } } return -1; } /** * 小米 ROM 權(quán)限申請(qǐng) */ public static void applyMiuiPermission(Context context) { int versionCode = getMiuiVersion(); if (versionCode == 5) { goToMiuiPermissionActivity_V5(context); } else if (versionCode == 6) { goToMiuiPermissionActivity_V6(context); } else if (versionCode == 7) { goToMiuiPermissionActivity_V7(context); } else if (versionCode == 8) { goToMiuiPermissionActivity_V8(context); } else { Log.e(TAG, "this is a special MIUI rom version, its version code " + versionCode); } } private static boolean isIntentAvailable(Intent intent, Context context) { if (intent == null) { return false; } return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0; } /** * 小米 V5 版本 ROM權(quán)限申請(qǐng) */ public static void goToMiuiPermissionActivity_V5(Context context) { Intent intent = null; String packageName = context.getPackageName(); intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package" , packageName, null); intent.setData(uri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (isIntentAvailable(intent, context)) { context.startActivity(intent); } else { Log.e(TAG, "intent is not available!"); } //設(shè)置頁(yè)面在應(yīng)用詳情頁(yè)面 // Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); // PackageInfo pInfo = null; // try { // pInfo = context.getPackageManager().getPackageInfo // (HostInterfaceManager.getHostInterface().getApp().getPackageName(), 0); // } catch (PackageManager.NameNotFoundException e) { // AVLogUtils.e(TAG, e.getMessage()); // } // intent.setClassName("com.android.settings", "com.miui.securitycenter.permission.AppPermissionsEditor"); // intent.putExtra("extra_package_uid", pInfo.applicationInfo.uid); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // if (isIntentAvailable(intent, context)) { // context.startActivity(intent); // } else { // AVLogUtils.e(TAG, "Intent is not available!"); // } } /** * 小米 V6 版本 ROM權(quán)限申請(qǐng) */ public static void goToMiuiPermissionActivity_V6(Context context) { Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity"); intent.putExtra("extra_pkgname", context.getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (isIntentAvailable(intent, context)) { context.startActivity(intent); } else { Log.e(TAG, "Intent is not available!"); } } /** * 小米 V7 版本 ROM權(quán)限申請(qǐng) */ public static void goToMiuiPermissionActivity_V7(Context context) { Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity"); intent.putExtra("extra_pkgname", context.getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (isIntentAvailable(intent, context)) { context.startActivity(intent); } else { Log.e(TAG, "Intent is not available!"); } } /** * 小米 V8 版本 ROM權(quán)限申請(qǐng) */ public static void goToMiuiPermissionActivity_V8(Context context) { Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity"); intent.putExtra("extra_pkgname", context.getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (isIntentAvailable(intent, context)) { context.startActivity(intent); } else { Log.e(TAG, "Intent is not available!"); } }
getSystemProperty 方法是直接調(diào)用 getprop 方法來(lái)獲取系統(tǒng)信息:
public static String getSystemProperty(String propName) { String line; BufferedReader input = null; try { Process p = Runtime.getRuntime().exec("getprop " + propName); input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); line = input.readLine(); input.close(); } catch (IOException ex) { Log.e(TAG, "Unable to read sysprop " + propName, ex); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { Log.e(TAG, "Exception while closing InputStream", e); } } } return line; }
最新的 V8 版本有些機(jī)型已經(jīng)是 6.0 ,所以就是下面介紹到 6.0 的適配方法了,感謝 @pinocchio2mx 的反饋,有些機(jī)型的 miui8 版本還是5.1.1,所以 miui8 依舊需要做適配,非常感謝,希望大家一起多多反饋問(wèn)題,謝謝~~。
魅族
魅族的適配,由于我司魅族的機(jī)器相對(duì)較少,所以只適配了 flyme5.1.1/android 5.1.1 版本 mx4 pro 的系統(tǒng)。和小米一樣,首先也要通過(guò) API19 版本添加的 AppOpsManager 類判斷是否授予了權(quán)限:
/** * 檢測(cè) meizu 懸浮窗權(quán)限 */ public static boolean checkFloatWindowPermission(Context context) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24; } return true; } @TargetApi(Build.VERSION_CODES.KITKAT) private 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 clazz = AppOpsManager.class; Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class); return AppOpsManager.MODE_ALLOWED == (int)method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName()); } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } } else { Log.e(TAG, "Below API 19 cannot invoke!"); } return false; }
然后是跳轉(zhuǎn)去懸浮窗權(quán)限授予界面:
/** * 去魅族權(quán)限申請(qǐng)頁(yè)面 */ public static void applyPermission(Context context){ Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC"); intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity"); intent.putExtra("packageName", context.getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); }
如果有魅族其他版本的適配方案,請(qǐng)聯(lián)系我。
華為
華為的適配是根據(jù)網(wǎng)上找的方案,外加自己的一些優(yōu)化而成,但是由于華為手機(jī)的眾多機(jī)型,所以覆蓋的機(jī)型和系統(tǒng)版本還不是那么全面,如果有其他機(jī)型和版本的適配方案,請(qǐng)聯(lián)系我,我更新到 github 上。和小米,魅族一樣,首先通過(guò) AppOpsManager 來(lái)判斷權(quán)限是否已經(jīng)授權(quán):
/** * 檢測(cè) Huawei 懸浮窗權(quán)限 */ public static boolean checkFloatWindowPermission(Context context) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24; } return true; } @TargetApi(Build.VERSION_CODES.KITKAT) private 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 clazz = AppOpsManager.class; Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class); return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName()); } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } } else { Log.e(TAG, "Below API 19 cannot invoke!"); } return false; }
然后根據(jù)不同的機(jī)型和版本跳轉(zhuǎn)到不同的頁(yè)面:
/** * 去華為權(quán)限申請(qǐng)頁(yè)面 */ public static void applyPermission(Context context) { try { Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity");//華為權(quán)限管理 // ComponentName comp = new ComponentName("com.huawei.systemmanager", // "com.huawei.permissionmanager.ui.SingleAppActivity");//華為權(quán)限管理,跳轉(zhuǎn)到指定app的權(quán)限管理位置需要華為接口權(quán)限,未解決 ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//懸浮窗管理頁(yè)面 intent.setComponent(comp); if (RomUtils.getEmuiVersion() == 3.1) { //emui 3.1 的適配 context.startActivity(intent); } else { //emui 3.0 的適配 comp = new ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity");//懸浮窗管理頁(yè)面 intent.setComponent(comp); context.startActivity(intent); } } catch (SecurityException e) { Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity");//華為權(quán)限管理 ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//華為權(quán)限管理,跳轉(zhuǎn)到本app的權(quán)限管理頁(yè)面,這個(gè)需要華為接口權(quán)限,未解決 // ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//懸浮窗管理頁(yè)面 intent.setComponent(comp); context.startActivity(intent); Log.e(TAG, Log.getStackTraceString(e)); } catch (ActivityNotFoundException e) { /** * 手機(jī)管家版本較低 HUAWEI SC-UL10 */ // Toast.makeText(MainActivity.this, "act找不到", Toast.LENGTH_LONG).show(); Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ComponentName comp = new ComponentName("com.Android.settings", "com.android.settings.permission.TabItem");//權(quán)限管理頁(yè)面 android4.4 // ComponentName comp = new ComponentName("com.android.settings","com.android.settings.permission.single_app_activity");//此處可跳轉(zhuǎn)到指定app對(duì)應(yīng)的權(quán)限管理頁(yè)面,但是需要相關(guān)權(quán)限,未解決 intent.setComponent(comp); context.startActivity(intent); e.printStackTrace(); Log.e(TAG, Log.getStackTraceString(e)); } catch (Exception e) { //拋出異常時(shí)提示信息 Toast.makeText(context, "進(jìn)入設(shè)置頁(yè)面失敗,請(qǐng)手動(dòng)設(shè)置", Toast.LENGTH_LONG).show(); Log.e(TAG, Log.getStackTraceString(e)); } }
emui4 之后就是 6.0 版本了,按照下面介紹的 6.0 適配方案即可。
360
360手機(jī)的適配方案在網(wǎng)上可以找到的資料很少,也沒(méi)有給出最后的適配方案,不過(guò)最后居然直接用最簡(jiǎn)單的辦法就能跳進(jìn)去了,首先是權(quán)限的檢測(cè):
/** * 檢測(cè) 360 懸浮窗權(quán)限 */ public static boolean checkFloatWindowPermission(Context context) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24; } return true; } @TargetApi(Build.VERSION_CODES.KITKAT) private 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 clazz = AppOpsManager.class; Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class); return AppOpsManager.MODE_ALLOWED == (int)method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName()); } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } } else { Log.e("", "Below API 19 cannot invoke!"); } return false; }
如果沒(méi)有授予懸浮窗權(quán)限,就跳轉(zhuǎn)去權(quán)限授予界面:
public static void applyPermission(Context context) { Intent intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.Settings$OverlaySettingsActivity"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); }
哈哈哈,是不是很簡(jiǎn)單,有時(shí)候真相往往一點(diǎn)也不復(fù)雜,OK,適配完成。
Android 6.0 及之后版本
懸浮窗權(quán)限在 6.0 之后就被 google 單獨(dú)拿出來(lái)管理了,好處就是對(duì)我們來(lái)說(shuō)適配就非常方便了,在所有手機(jī)和 6.0 以及之后的版本上適配的方法都是一樣的,首先要在 Manifest 中靜態(tài)申請(qǐng)<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
權(quán)限,然后在使用時(shí)先判斷該權(quán)限是否已經(jīng)被授權(quán),如果沒(méi)有授權(quán)使用下面這段代碼進(jìn)行動(dòng)態(tài)申請(qǐng):
private static final int REQUEST_CODE = 1; //判斷權(quán)限 private boolean commonROMPermissionCheck(Context context) { Boolean result = true; if (Build.VERSION.SDK_INT >= 23) { try { Class clazz = Settings.class; Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class); result = (Boolean) canDrawOverlays.invoke(null, context); } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } } return result; } //申請(qǐng)權(quán)限 private void requestAlertWindowPermission() { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, REQUEST_CODE); } @Override //處理回調(diào) protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { if (Settings.canDrawOverlays(this)) { Log.i(LOGTAG, "onActivityResult granted"); } } }
上述代碼需要注意的是:
使用Action Settings.ACTION_MANAGE_OVERLAY_PERMISSION 啟動(dòng)隱式Intent;
使用 “package:” + getPackageName() 攜帶App的包名信息;
使用 Settings.canDrawOverlays 方法判斷授權(quán)結(jié)果。
在用戶開(kāi)啟相關(guān)權(quán)限之后才能使用 WindowManager.LayoutParams.TYPE_SYSTEM_ERROR ,要不然是會(huì)直接崩潰的哦。
特殊適配流程
如何繞過(guò)系統(tǒng)的權(quán)限檢查,直接彈出懸浮窗?需要使用mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
來(lái)取代 mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
,這樣就可以達(dá)到不申請(qǐng)權(quán)限,而直接彈出懸浮窗,至于原因嘛,我們看看 PhoneWindowManager 源碼的關(guān)鍵處:
@Override public int checkAddPermission(WindowManager.LayoutParams attrs, int[] outAppOp) { .... switch (type) { case TYPE_TOAST: // XXX right now the app process has complete control over // this... should introduce a token to let the system // monitor/control what they are doing. outAppOp[0] = AppOpsManager.OP_TOAST_WINDOW; break; case TYPE_DREAM: case TYPE_INPUT_METHOD: case TYPE_WALLPAPER: case TYPE_PRIVATE_PRESENTATION: case TYPE_VOICE_INTERACTION: case TYPE_ACCESSIBILITY_OVERLAY: // The window manager will check these. break; case TYPE_PHONE: case TYPE_PRIORITY_PHONE: case TYPE_SYSTEM_ALERT: case TYPE_SYSTEM_ERROR: case TYPE_SYSTEM_OVERLAY: permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW; outAppOp[0] = AppOpsManager.OP_SYSTEM_ALERT_WINDOW; break; default: permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW; } if (permission != null) { if (permission == android.Manifest.permission.SYSTEM_ALERT_WINDOW) { final int callingUid = Binder.getCallingUid(); // system processes will be automatically allowed privilege to draw if (callingUid == Process.SYSTEM_UID) { return WindowManagerGlobal.ADD_OKAY; } // check if user has enabled this operation. SecurityException will be thrown if // this app has not been allowed by the user final int mode = mAppOpsManager.checkOp(outAppOp[0], callingUid, attrs.packageName); switch (mode) { case AppOpsManager.MODE_ALLOWED: case AppOpsManager.MODE_IGNORED: // although we return ADD_OKAY for MODE_IGNORED, the added window will // actually be hidden in WindowManagerService return WindowManagerGlobal.ADD_OKAY; case AppOpsManager.MODE_ERRORED: return WindowManagerGlobal.ADD_PERMISSION_DENIED; default: // in the default mode, we will make a decision here based on // checkCallingPermission() if (mContext.checkCallingPermission(permission) != PackageManager.PERMISSION_GRANTED) { return WindowManagerGlobal.ADD_PERMISSION_DENIED; } else { return WindowManagerGlobal.ADD_OKAY; } } } if (mContext.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { return WindowManagerGlobal.ADD_PERMISSION_DENIED; } } return WindowManagerGlobal.ADD_OKAY; }
從源碼中可以看到,其實(shí) TYPE_TOAST 沒(méi)有做權(quán)限檢查,直接返回了 WindowManagerGlobal.ADD_OKAY,所以呢,這就是為什么可以繞過(guò)權(quán)限的原因。還有需要注意的一點(diǎn)是 addView 方法中會(huì)調(diào)用到 mPolicy.adjustWindowParamsLw(win.mAttrs);
,這個(gè)方法在不同的版本有不同的實(shí)現(xiàn):
//Android 2.0 - 2.3.7 PhoneWindowManager public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) { switch (attrs.type) { case TYPE_SYSTEM_OVERLAY: case TYPE_SECURE_SYSTEM_OVERLAY: case TYPE_TOAST: // These types of windows can't receive input events. attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; break; } } //Android 4.0.1 - 4.3.1 PhoneWindowManager public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) { switch (attrs.type) { case TYPE_SYSTEM_OVERLAY: case TYPE_SECURE_SYSTEM_OVERLAY: case TYPE_TOAST: // These types of windows can't receive input events. attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; attrs.flags &= ~WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; break; } } //Android 4.4 PhoneWindowManager @Override public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) { switch (attrs.type) { case TYPE_SYSTEM_OVERLAY: case TYPE_SECURE_SYSTEM_OVERLAY: // These types of windows can't receive input events. attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; attrs.flags &= ~WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; break; } }
可以看到,在4.0.1以前, 當(dāng)我們使用 TYPE_TOAST, Android 會(huì)偷偷給我們加上 FLAG_NOT_FOCUSABLE 和 FLAG_NOT_TOUCHABLE,4.0.1 開(kāi)始,會(huì)額外再去掉FLAG_WATCH_OUTSIDE_TOUCH,這樣真的是什么事件都沒(méi)了。而 4.4 開(kāi)始,TYPE_TOAST 被移除了, 所以從 4.4 開(kāi)始,使用 TYPE_TOAST 的同時(shí)還可以接收觸摸事件和按鍵事件了,而4.4以前只能顯示出來(lái),不能交互,所以 API18 及以下使用 TYPE_TOAST 是無(wú)法接收觸摸事件的,但是幸運(yùn)的是除了 miui 之外,這些版本可以直接在 Manifest 文件中聲明 android.permission.SYSTEM_ALERT_WINDOW
權(quán)限,然后直接使用 WindowManager.LayoutParams.TYPE_PHONE
或者 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
都是可以直接彈出懸浮窗的。
還有一個(gè)需要提到的是 TYPE_APPLICATION
,這個(gè) type 是配合 Activity 在當(dāng)前 APP 內(nèi)部使用的,也就是說(shuō),回到 Launcher 界面,這個(gè)懸浮窗是會(huì)消失的。
雖然這種方法確確實(shí)實(shí)可以繞過(guò)權(quán)限,至于適配的坑呢,有人遇到之后可以聯(lián)系我,我會(huì)持續(xù)完善。不過(guò)由于這樣可以不申請(qǐng)權(quán)限就彈出懸浮窗,而且在最新的 6.0+ 系統(tǒng)上也沒(méi)有修復(fù),所以如果這個(gè)漏洞被濫用,就會(huì)造成一些意想不到的后果,因此我個(gè)人傾向于使用 QQ 的適配方案,也就是上面的正常適配流程去處理這個(gè)權(quán)限。
更新:7.1.1之后版本
最新發(fā)現(xiàn)在 7.1.1 版本之后使用 type_toast 重復(fù)添加兩次懸浮窗,第二次會(huì)崩潰,跑出來(lái)下面的錯(cuò)誤:
E/AndroidRuntime: FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@d7a4e96 has already been added at android.view.ViewRootImpl.setView(ViewRootImpl.java:691) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at com.tencent.ysdk.module.icon.impl.a.g(Unknown Source) at com.tencent.ysdk.module.icon.impl.floatingviews.q.onAnimationEnd(Unknown Source) at android.view.animation.Animation$3.run(Animation.java:381) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
去追溯源碼,發(fā)現(xiàn)是這里拋出來(lái)的錯(cuò)誤:
try { mOrigWindowType = mWindowAttributes.type; mAttachInfo.mRecomputeGlobalAttributes = true; collectViewAttributes(); res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(), mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, mAttachInfo.mOutsets, mInputChannel); } catch (RemoteException e) { ..... } finally { if (restore) { attrs.restore(); } } ..... if (res < WindowManagerGlobal.ADD_OKAY) { ..... switch (res) { .... case WindowManagerGlobal.ADD_DUPLICATE_ADD: throw new WindowManager.BadTokenException( "Unable to add window -- window " + mWindow + " has already been added"); } }
然后去查看拋出這個(gè)異常處的代碼:
if (mWindowMap.containsKey(client.asBinder())) { Slog.w(TAG_WM, "Window " + client + " is already added"); return WindowManagerGlobal.ADD_DUPLICATE_ADD; }
然后我們從 mWindowMap 這個(gè)變量出發(fā)去分析,但是最后發(fā)現(xiàn),根本不行,這些代碼從 5.X 版本就存在了,而且每次調(diào)用 addview 方法去添加一個(gè) view 的時(shí)候,都是一個(gè)新的 client 對(duì)象,所以 mWindowMap.containsKey(client.asBinder())
一直是不成立的,所以無(wú)法從這里去分析,于是繼續(xù)分析在 7.0 版本是沒(méi)有問(wèn)題的,但是在 7.1.1 版本就出現(xiàn)問(wèn)題了,所以我們?nèi)ゲ榭?7.1.1 版本代碼的變更:
https://android.googlesource.com/platform/frameworks/base/+log/master/services/core/java/com/android/server/wm/WindowManagerService.java?s=28f0e5bf48e2d02e1f063670e435b1232f07ba03
我們從里面尋找關(guān)于 type_toast 的相關(guān)變更:
最終定位到了 aa07653 那個(gè)提交,我們看看這次提交修改的內(nèi)容:
然后點(diǎn)開(kāi) WMS 的修改:
去到 canAddToastWindowForUid:
我們于是定位到了關(guān)鍵 7.1.1 上面不能重復(fù)添加 type_toast 類型 window 的原因!!
另外還有一點(diǎn)需要注意的是,在 7.1.1 上面還增加了如下的代碼:
可以看到在 25 版本之后,注意是之后,也就是 8.0,系統(tǒng)將會(huì)限制 type_toast 的使用,會(huì)直接拋出異常,這也是需要注意的地方。
最新適配結(jié)果
非常感謝ruanqin0706同學(xué)的大力幫忙,通過(guò)優(yōu)測(cè)網(wǎng)的機(jī)型的測(cè)試適配,現(xiàn)在統(tǒng)計(jì)結(jié)果如下所示:
6.0/6.0+
更新,6.0魅族的適配方案不能使用google API,依舊要使用 6.0 之前的適配方法,已經(jīng)適配完成~
6.0 上絕大部分的機(jī)型都是可以的,除了魅族這種奇葩機(jī)型:
機(jī)型 | 版本 | 詳細(xì)信息 | 適配完成 | 具體表現(xiàn) |
---|---|---|---|---|
魅族 PRO6 | 6.0 | 型號(hào):PRO6;版本:6.0;分辨率:1920*1080 | 否 | 檢測(cè)權(quán)限結(jié)果有誤,微信可正常縮小放大,而我方檢測(cè)為未開(kāi)啟權(quán)限,為跳轉(zhuǎn)至開(kāi)啟權(quán)限頁(yè) |
魅族 U20 | 6.0 | 型號(hào):U20;版本:6.0;分辨率:1920*1080 | 否 | 檢測(cè)權(quán)限結(jié)果有誤,微信可正常縮小放大,而我方檢測(cè)為未開(kāi)啟權(quán)限,為跳轉(zhuǎn)至開(kāi)啟權(quán)限頁(yè) |
結(jié)論:
匯總結(jié)果 |
---|
Android6.0 及以上機(jī)型覆蓋:58款,其中: |
三星:10款,均正常 |
華為:21款,均正常 |
小米:5款,均正常 |
魅族:2款,異常(1.檢測(cè)權(quán)限未開(kāi)啟,點(diǎn)擊 Android 6.0 及以上跳轉(zhuǎn),無(wú)法跳轉(zhuǎn),卻可以選擇魅族手機(jī)設(shè)置,設(shè)置后,懸浮窗打開(kāi)縮小正常;2.在魅族上,及時(shí)設(shè)置懸浮窗關(guān)閉,微信也可正常縮小,但是我們檢測(cè)的懸浮窗是否開(kāi)發(fā)結(jié)果,和實(shí)際系統(tǒng)的設(shè)置是匹配的。) |
其他:20款,均正常 |
已適配完成,針對(duì)魅族的手機(jī),在 6.0 之后仍然使用老的跳轉(zhuǎn)方式,而不是使用新版本的 Google API 進(jìn)行跳轉(zhuǎn)。
huawei
這里是華為手機(jī)的測(cè)試結(jié)果:
機(jī)型 | 版本 | 適配完成 | 具體表現(xiàn) | 默認(rèn)設(shè)置 |
---|---|---|---|---|
華為榮耀x2 | 5.0 | 否 | 跳轉(zhuǎn)至通知中心頁(yè)面,而非懸浮窗管理處 | 默認(rèn)關(guān)閉 |
華為暢玩4x(電信版) | 4.4.4 | 可以優(yōu)化 | 跳轉(zhuǎn)至通知中心標(biāo)簽頁(yè)面,用戶需切換標(biāo)簽頁(yè)(通知中心、懸浮窗為兩個(gè)不同標(biāo)簽頁(yè)) | 默認(rèn)關(guān)閉 |
華為 p8 lite | 4.4.4 | 可以優(yōu)化 | 跳轉(zhuǎn)至通知中心標(biāo)簽頁(yè)面,用戶需切換標(biāo)簽頁(yè)(通知中心、懸浮窗為兩個(gè)不同標(biāo)簽頁(yè)) | 默認(rèn)關(guān)閉 |
華為榮耀 6 移動(dòng)版 | 4.4.2 | 可以優(yōu)化 | 跳轉(zhuǎn)至通知中心標(biāo)簽頁(yè)面,用戶需切換標(biāo)簽頁(yè)(通知中心、懸浮窗為兩個(gè)不同標(biāo)簽頁(yè)) | 默認(rèn)關(guān)閉 |
華為榮耀 3c 電信版 | 4.3 | 是 | 跳轉(zhuǎn)至通知中心,但默認(rèn)是開(kāi)啟懸浮窗的 | 默認(rèn)關(guān)閉 |
華為 G520 | 4.1.2 | 否 | 直接點(diǎn)擊華為跳轉(zhuǎn)設(shè)置頁(yè)按鈕,閃退 | 默認(rèn)開(kāi)啟 |
結(jié)論:
匯總結(jié)果 | 完全兼容機(jī)型數(shù)量 | 次兼容機(jī)型數(shù)量 | 總測(cè)試機(jī)型數(shù) | 兼容成功率 |
---|---|---|---|---|
華為6.0以下機(jī)型覆蓋:18款,其中: 5.0.1以上:11款,均默認(rèn)開(kāi)啟,且跳轉(zhuǎn)設(shè)置頁(yè)面正確;5.0:1款,處理異常 (默認(rèn)未開(kāi)啟懸浮窗權(quán)限,且點(diǎn)擊跳轉(zhuǎn)至通知欄,非懸浮窗設(shè)置入口) 4.4.4、4.4.2:3款,處理可接受 (默認(rèn)未開(kāi)啟懸浮窗權(quán)限,點(diǎn)擊跳轉(zhuǎn)至通知中心的“通知欄”標(biāo)簽頁(yè),可手動(dòng)切換至“懸浮窗”標(biāo)簽頁(yè)設(shè)置) 4.3:1款,處理可接受 (默認(rèn)開(kāi)啟,但點(diǎn)擊華為跳轉(zhuǎn)設(shè)置頁(yè),跳轉(zhuǎn)至通知中心,無(wú)懸浮窗設(shè)置處) 4.2.2:1款,默認(rèn)開(kāi)啟,處理正常 4.1.2:1款,處理有瑕疵 (默認(rèn)開(kāi)啟,但若直接點(diǎn)擊華為跳轉(zhuǎn)按鈕,出現(xiàn)閃退) | 12 | 5 | 18 | 94.44% |
正在適配中…
xiaomi
大部分的小米機(jī)型都是可以成功適配,除了某些奇怪的機(jī)型:
機(jī)型 | 版本 | 適配完成 | 具體表現(xiàn) |
---|---|---|---|
小米 MI 4S | 5.1.1 | 否 | 無(wú)懸浮窗權(quán)限,點(diǎn)擊小米手機(jī)授權(quán)頁(yè)跳轉(zhuǎn)按鈕,無(wú)反應(yīng) |
小米 紅米NOTE 1S | 4.4.4 | 未執(zhí)行 | 未修改開(kāi)啟懸浮窗成功,真機(jī)平臺(tái)不支持(為權(quán)限與之前系統(tǒng)有別) |
小米 紅米1(聯(lián)通版) | 4.2.2 | 未執(zhí)行 | 未安裝成功 |
結(jié)論:
匯總結(jié)果 | 完全兼容機(jī)型數(shù)量 | 次兼容機(jī)型數(shù)量 | 總測(cè)試機(jī)型數(shù) | 兼容成功率 |
---|---|---|---|---|
小米6.0以下機(jī)型覆蓋:10款,其中: 5.1.1 小米 MI 4S:1款,兼容失敗 (默認(rèn)未開(kāi)啟,點(diǎn)擊小米手機(jī)授權(quán)按鈕,無(wú)跳轉(zhuǎn)) 其他:9款,均成功 | 9 | 0 | 10 | 90% |
samsung
幾乎 100% 的機(jī)型都是配完美,結(jié)論:
匯總結(jié)果 | 完全兼容機(jī)型數(shù)量 | 次兼容機(jī)型數(shù)量 | 總測(cè)試機(jī)型數(shù) | 兼容成功率 |
---|---|---|---|---|
三星6.0以下機(jī)型覆蓋:28款,全部檢測(cè)處理成功 (默認(rèn)均開(kāi)啟懸浮窗權(quán)限) | 28 | 0 | 28 | 100% |
oppo&&vivo
藍(lán)綠大廠的機(jī)器,只測(cè)試了幾款機(jī)型,都是OK的:
機(jī)型 | 版本 | 適配完成 | 是否默認(rèn)開(kāi)啟 |
---|---|---|---|
OPPO R7sm | 5.1.1 | 是 | 默認(rèn)開(kāi)啟 |
OPPO R7 Plus | 5.0 | 是 | 默認(rèn)開(kāi)啟 |
OPPO R7 Plus(全網(wǎng)通) | 5.1.1 | 是 | 默認(rèn)開(kāi)啟 |
OPPO A37m | 5.1 | 未執(zhí)行 | 默認(rèn)未開(kāi)啟,且無(wú)法設(shè)置開(kāi)啟(平臺(tái)真機(jī)限制修改權(quán)限導(dǎo)致) |
OPPO A59m | 5.1.1 | 是 | 默認(rèn)開(kāi)啟 |
結(jié)論:
匯總結(jié)果 |
---|
抽查3款,2個(gè)系統(tǒng)版本,均兼容,100% |
others
其他的機(jī)型,HTC 和 Sony 大法之類的機(jī)器,隨機(jī)抽取了幾款,也都是 OK 的:
機(jī)型 | 是否正常 |
---|---|
藍(lán)魔 R3 | 是 |
HTC A9 | 是 |
摩托羅拉 Nexus 6 | 是 |
VIVO V3Max A | 是 |
金立 M5 | 是 |
HTC One E8 | 是 |
努比亞 Z11 Max | 是 |
Sony Xperia Z3+ Dual | 是 |
酷派 大神Note3 | 是 |
三星 GALAXY J3 Pro(雙4G) | 是 |
三星 Note 5 | 是 |
中興 威武3 | 是 |
中興 Axon Mini | 是 |
關(guān)于Android中怎么實(shí)現(xiàn)懸浮窗權(quán)限問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)站欄目:Android中怎么實(shí)現(xiàn)懸浮窗權(quán)限
本文URL:http://www.2m8n56k.cn/article18/jcgsgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)