前段时间,项目需要做一个类似于360加速器的东西,Android机型这么多,好多手机都把这个权限给默认关闭掉了,处理这个问题真的好麻烦,不过最后还是解决了这个问题。
接下来我将和大家分享下我是怎么解决这个问题呢。
1.悬浮窗主要用到的技术是WindowManager,设置WindowManager.LayoutParams是关键
我首先设置的params.type = LayoutParams.TYPE_PHONE;是这个,但是这个的话需要android.permission.SYSTEM_ALERT_WINDOW这个权限,然而小米的MIUI系统这个权限默认是关闭的,需要用户手动去打开。(小米的用户那么多,这样肯定是不可行的)
接着我又试了设置params.type = LayoutParams.TYPE_TOAST,但是这个的话华为手机又获取不到焦点,真是麻烦,Android手机真是多
后来我想了想这样写看看是否可行
String packname = context.getPackageName();
PackageManager pm = context.getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname));
if(permission){
params.type = LayoutParams.TYPE_PHONE;
}else{
params.type = LayoutParams.TYPE_TOAST;
}
最后经过测试,这样写真的可行,测试帮我试了几十种常见的手机,最后发现没有问题,大功告成。
2.网上下了个360悬浮窗的Demo修改了下,供大家参考
我主要修改的地方是
public static void createSmallWindow(Context context) {
WindowManager windowManager = getWindowManager(context);
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
if (smallWindow == null) {
smallWindow = new FloatWindowSmallView(context);
if (smallWindowParams == null) {
smallWindowParams = new LayoutParams();
String packname = context.getPackageName();
PackageManager pm = context.getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname));
if(permission){
smallWindowParams.type = LayoutParams.TYPE_PHONE;
}else{
smallWindowParams.type = LayoutParams.TYPE_TOAST;
}
smallWindowParams.format = PixelFormat.RGBA_8888;
smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
smallWindowParams.width = FloatWindowSmallView.viewWidth;
smallWindowParams.height = FloatWindowSmallView.viewHeight;
smallWindowParams.x = screenWidth;
smallWindowParams.y = screenHeight / 2;
}
smallWindow.setParams(smallWindowParams);
windowManager.addView(smallWindow, smallWindowParams);
}
}
public static void createBigWindow(Context context) {
WindowManager windowManager = getWindowManager(context);
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
if (bigWindow == null) {
bigWindow = new FloatWindowBigView(context);
if (bigWindowParams == null) {
bigWindowParams = new LayoutParams();
bigWindowParams.x = screenWidth / 2 - FloatWindowBigView.viewWidth / 2;
bigWindowParams.y = screenHeight / 2 - FloatWindowBigView.viewHeight / 2;
String packname = context.getPackageName();
PackageManager pm = context.getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname));
if(permission){
bigWindowParams.type = LayoutParams.TYPE_PHONE;
}else{
bigWindowParams.type = LayoutParams.TYPE_TOAST;
}
bigWindowParams.format = PixelFormat.RGBA_8888;
bigWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
bigWindowParams.width = FloatWindowBigView.viewWidth;
bigWindowParams.height = FloatWindowBigView.viewHeight;
}
windowManager.addView(bigWindow, bigWindowParams);
}
}