悬浮窗(穿透实现)

1、实现效果文字描述

图片作为背景,使用悬浮窗实现,不影响下一层的实现,可以点击其中的软件等。

2、实现效果图片实现

在这里插入图片描述

3、实现步骤

3.1 manifest.xm中的操作(权限的声明)
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

3.2 悬浮窗的权限(自动开启)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && !Settings.canDrawOverlays(this)) {
			Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
			startActivityForResult(intent, 100);
		}
3.2 具体代码实现
		setContentView(R.layout.activity_button);
		Button button = findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startService(new Intent(getApplicationContext(),EyeServices.class));
			}
		});

		View root = View.inflate(this,R.layout.activity_main, null);
		//检查是否有悬浮权限
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && !Settings.canDrawOverlays(this)) {
			Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
			startActivityForResult(intent, 100);
		}
		WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
		WindowManager manager = null;
		manager = (WindowManager) getApplication().getSystemService(WINDOW_SERVICE);
		layoutParams.type = Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
				WindowManager.LayoutParams.TYPE_SYSTEM_ALERT : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
		//设置view不可焦,不可触
		layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
		;
		layoutParams.format = PixelFormat.TRANSLUCENT;
		layoutParams.x = WindowManager.LayoutParams.MATCH_PARENT;
		layoutParams.y = WindowManager.LayoutParams.MATCH_PARENT;
		manager.addView(root,layoutParams);

遇到的问题

4.1 manager.addView(root,layoutParams);无法addview
问题:未设置format
解决:增加layoutParams.format = PixelFormat.TRANSLUCENT;
4.2 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY没有权限
解决:在manifest添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值