一般用到的方式是
WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);//获取WindowManager
WindowManager.LayoutParams wlp = new WindowManager.LayoutParams();//悬浮窗参数,悬浮窗显示后变大变小也可以用这个取update
wlp.format = PixelFormat.TRANSLUCENT;
wlp.gravity = Gravity.CENTER;
if(Build.VERSION.SDK_INT >= 26){//android 8.0后要使用另外一个,否则会报permission denied
wlp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else{
wlp.type = WindowManager.LayoutParams.TYPE_PHONE;
}
wlp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
RelativeLayout customLayout = (Relative)View.inflate(this, R.layout.custom_layout, null);//自定义的View,悬浮窗内容
mWindowManager.addView(customLayout, wlp);//添加悬浮窗
//移除悬浮窗
mWindowManager.removeView(customLayout);
//更新悬浮窗
mWindowManager.updateViewLayout(customLayout, wlp);
这个需要权限,用到的权限为
android.permission.SYSTEM_ALERT_WINDOW
注意一下,Android 6.0以上需要动态申请权限
权限申请适配可以参考一下这篇博文
https://www.cnblogs.com/zhujiabin/p/9172848.html
里面涉及到相关的界面跳转,主要针对不同厂家进行适配