android悬浮窗

##悬浮窗具体的做法

小米5,7.0

1
2
3
4
5
6
7
if (Build.VERSION.SDK_INT \>= Build.VERSION_CODES.O) {
param.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else if (Build.VERSION.SDK_INT \>= Build.VERSION_CODES.N){
param.type = WindowManager.LayoutParams.TYPE_PHONE;//判断是否是洗小米系统,如果是,使用toast.settext去实现
} else {
param.type = WindowManager.LayoutParams.TYPE_TOAST;
}

https://blog.csdn.net/yhaolpz/article/details/78527803

https://github.com/yhaolpz/FloatWindow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams param = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
param.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
param.type = WindowManager.LayoutParams.TYPE_PHONE;
} else {
param.type = WindowManager.LayoutParams.TYPE_TOAST;
}
param.format = PixelFormat.TRANSLUCENT;
param.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; // 不能抢占聚焦点
param.flags = param.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
param.flags = param.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; // 排版不受限制

param.alpha = 1f;
param.gravity = Gravity.CENTER; //调整悬浮窗口至左上角
param.x = 0;
param.y = 0;
final View v = LayoutInflater.from(context).inflate(R.layout.dialog_buy_share, null);

Button btnPopup = (Button) v.findViewById(R.id.confirm_btn);
Button btnCancel = (Button) v.findViewById(R.id.cancel_btn);

if (!TextUtils.isEmpty(bean.getButtonText())) {
btnPopup.setText(bean.getButtonText());
}

btnPopup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
StatisticsUtils.statisticsEvent(context, "购买成功页popup_点击_分享", "活动名称", "邀请");
windowManager.removeView(v);
if (bean != null && ActivityManager.getInstance().currentActivity() != null) {
RedirectActivityUtils.redirectPage(ActivityManager.getInstance().currentActivity(), bean);
// ActivityMapManager.finishActivity(InvestSuccessActivity.class.getSimpleName());
// ActivityMapManager.finishActivity(BuyFixResultCountDownActivity.class.getSimpleName());
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
windowManager.removeView(v);
}
});
if (windowManager != null) {
windowManager.addView(v, param);
}