当前位置: 首页 > news >正文

免费网站cms上海缪斯设计公司地址

免费网站cms,上海缪斯设计公司地址,成都餐饮vi设计公司,禅城专业网站建设公司一、无障碍功能简介 首先我们先来了解下无障碍功能的官方介绍: 无障碍服务仅应用于帮助残障用户使用 Android 设备和应用。它们在后台运行,并在触发 AccessibilityEvents 时接收系统的回调。此类事件表示用户界面中的某些状态转换,例如焦点已…

一、无障碍功能简介

首先我们先来了解下无障碍功能的官方介绍:

         无障碍服务仅应用于帮助残障用户使用 Android 设备和应用。它们在后台运行,并在触发 AccessibilityEvents 时接收系统的回调。此类事件表示用户界面中的某些状态转换,例如焦点已更改、按钮已被点击等。此类服务可以选择性地请求查询活动窗口内容的功能。开发无障碍服务需要扩展此类并实现其抽象方法。

        从以上介绍我们得知可以通过这个系统服务,添加全局的活动窗口,并对窗口状态做监听。那么我们今天就借助系统的无障碍服务来实现的设备全局水印效果,具体的需求内容是添加一个全局的文字透明层,同时确保不抢占页面焦点,并允许用户在其他应用上继续操作界面。说白了,我们要实现的全局水印效果就是实现一个顶层的透明层VIEW,只显示出来并不影响应用用户设备操作,同时给设备加水印,防止设备具体内容被剽窃,以保证达到设备安全的目的。

二、设备水印功能实现

在内容实现之前,我们先来了解一下几个Window窗口相关的属性。

窗口类型:WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY ;这种类型的窗口专门为无障碍服务设计,能覆盖在所有应用之上而不会影响用户操作。

窗口标志:WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;让窗口永远不会获得按键输入焦点,因此用户无法向其发送按键或其他按钮事件。

窗口标志:WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;让窗口永远不能接收触摸事件。此标志的目的是让位于此窗口下方的某个窗口(按 Z 顺序)来处理触摸。

窗口标志:WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;该标志可以将窗口放置在整个屏幕内,忽略来自父窗口的任何限制。

1、 创建无障碍服务

首先创建一个无障碍服务(Accessibility Service),该服务将用于显示全局的透明水印文字层。

class CustomOverlayService: AccessibilityService() {private lateinit var overlayWindowManager: WindowManagerprivate var overlayView: View? = nulloverride fun onServiceConnected() {super.onServiceConnected()// 初始化WindowManageroverlayWindowManager = getSystemService(WINDOW_SERVICE) as WindowManager// 加载布局overlayView = LayoutInflater.from(this).inflate(R.layout.layout_watermark, null)// 设置布局参数val params: WindowManager.LayoutParams = WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,  // 无障碍服务专用类型WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or  // 不抢占焦点WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or  // 不可触摸WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,  // 显示在屏幕上PixelFormat.TRANSLUCENT)// 设置位置,例如屏幕中央params.gravity = Gravity.CENTER// 添加到WindowManageroverlayWindowManager.addView(overlayView, params)}override fun onDestroy() {super.onDestroy()if (overlayView != null) {overlayWindowManager.removeView(overlayView)}}@RequiresApi(Build.VERSION_CODES.N)override fun onAccessibilityEvent(event: AccessibilityEvent?) {// 无障碍事件处理,不做任何操作}override fun onInterrupt() {// 中断时的处理}
}

2、定义布局文件

创建显示在Window顶层的透明水印显示布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:id="@+id/layout1"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:layout_marginTop="20dp"><TextViewandroid:layout_width="0dp"android:layout_height="100dp"android:layout_weight="1"android:gravity="center"android:rotation="50.0"android:text="Hello 2025"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /></LinearLayout><LinearLayoutandroid:id="@+id/layout2"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:layout_marginTop="50dp"><TextViewandroid:layout_width="0dp"android:layout_height="119dp"android:layout_weight="1"android:gravity="center"android:rotation="50.0"android:text="Hello 2025"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /></LinearLayout><LinearLayoutandroid:id="@+id/layout3"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:layout_marginTop="50dp"><TextViewandroid:layout_width="0dp"android:layout_height="119dp"android:layout_weight="1"android:gravity="center"android:rotation="50.0"android:text="Hello 2025"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /></LinearLayout><LinearLayoutandroid:id="@+id/layout4"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:layout_marginTop="50dp"><TextViewandroid:layout_width="0dp"android:layout_height="119dp"android:layout_weight="1"android:gravity="center"android:rotation="50.0"android:text="Hello 2025"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="center"android:text="Hello 2025"android:rotation="50.0"android:textColor="@color/light_gray"android:textSize="20sp" /></LinearLayout></LinearLayout>

3、在AndroidManifest.xml 中声明无障碍服务

在应用清单文件AndroidManifest.xml 中声明无障碍服务,并配置相关的无障碍权限。

        <serviceandroid:name=".CustomOverlayService"android:exported="true"android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"><intent-filter><action android:name="android.accessibilityservice.AccessibilityService" /></intent-filter><meta-dataandroid:name="android.accessibilityservice"android:resource="@xml/accessibility_service_config" /></service>

另外,上面的android:resource引用的无障碍属性配置文件,需要在 res/xml/ 文件夹中创建 accessibility_service_config.xml 文件,去配置无障碍服务的相关属性。

<accessibility-servicexmlns:android="http://schemas.android.com/apk/res/android"android:description="@string/accessibility_service_descriptions"android:accessibilityEventTypes="typeAllMask"android:canRetrieveWindowContent="true"android:packageNames=""android:notificationTimeout="100"android:accessibilityFeedbackType="feedbackAllMask"android:accessibilityFlags="flagDefault"android:settingsActivity=""/>

4、启用无障碍服务

写一个页面,添加一个开启无障碍服务的按钮,点击按钮跳转到系统设置中的辅助功能,可以去开启对应应用的无障碍功能开关。具体操作路径为:设置 -> 辅助功能 -> 已安装的服务 中找到你的服务并开启它。

class MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {Greeting()}}
}@Composable
fun Greeting() {Column(modifier = Modifier.fillMaxSize(),horizontalAlignment = Alignment.CenterHorizontally,verticalArrangement = Arrangement.Center) {val context: Context = LocalContext.currentButton(onClick = {if (!isAccessibilityServiceEnabled(context, CustomOverlayService::class.java)) {Log.i("AccessibilityService", "AccessibilityService disabled .")context.startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))} else {Log.i("AccessibilityService", "AccessibilityService enabled .")context.startService(Intent(context,CustomOverlayService::class.java))}}, shape = ButtonDefaults.textShape) {Text(text = "启动无障碍功能")}}
}/*** 判断无障碍服务是否开启*/
private fun isAccessibilityServiceEnabled(context: Context, accessibilityServiceClass: Class<*>): Boolean {var accessibilityEnabled = 0val service: String = context.packageName.toString() + "/" + accessibilityServiceClass.canonicalNametry {accessibilityEnabled = Settings.Secure.getInt(context.contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)} catch (e: Settings.SettingNotFoundException) {e.printStackTrace()}val colonSplitter = TextUtils.SimpleStringSplitter(':')if (accessibilityEnabled == 1) {val settingValue = Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)if (settingValue != null) {colonSplitter.setString(settingValue)while (colonSplitter.hasNext()) {val componentName = colonSplitter.next()if (componentName.equals(service, ignoreCase = true)) {return true}}}}return false
}

5、设备全局水印效果展示

系统设置页面水印显示效果
系统设置页面水印

设备锁屏页面水印

三、总结

         无障碍服务权限是一个非常强大的工具,开启后可以实现很多你意想不到的效果或者功能。比如,还可以通过无障碍服务获取设备上运行的最上层应用的包名以及VIEW。后面有空的话,我也会深挖更多的无障碍服务相关的功能来展示给大家,敬请期待!

demo源码,请查看文章开头资源链接。

http://www.yayakq.cn/news/603086/

相关文章:

  • 自适应网站建设沈阳wordpress侧栏登录
  • 温州做网站最好的中国建设银行网站口
  • asp.net 跳转别的网站制作网页的步骤800字
  • 承接网站建设文案电商系统哪家好排行榜
  • 阿里巴巴可以做网站吗公司网站建设的建议
  • 荧光字体制作网站微信企业号可以做微网站吗
  • 济南做网站公司网推接单网
  • 服装网站开发宠物网站推广怎么做
  • 公司网站的主页优化公众号取名神器
  • 网站开发图片编辑深圳个性化网站建设公司电话
  • 如何做网站关键词词霸我要做网店官网
  • 网站内备案名称 修改做网站论文研究方法怎么写
  • 茌平网站建设电话建设公积金网站
  • 网站dns解析设置网站开发有哪些
  • 郑州企业网站seowordpress的网站
  • 网站推广的短视频推广京东云服务器
  • 服装购物网站建设最新wordpress教程视频教程
  • 免费自动网页生成器榜单优化
  • 在线建网址河南seo推广
  • 多页网站制作专业的外贸网站建设公司价格
  • 求个网站带图片素材网站建设是什么软件
  • 江苏卓业建设网站甘肃网络营销是什么
  • 德阳城乡建设部网站首页网站建设销售培训语
  • 海南专业网站运营托管wordpress首页按钮
  • 中国知名设计网站网站后台怎么打开
  • seo包括网站建设吗wordpress新建文章页面
  • 简单的旅游网站怎么做网站推广营销收费
  • 网站刚刚开始怎么做获得网站后台地址
  • 软件班级网站建设域名空间申请
  • 酒店网站 方案河北建设工程信息网官网官网官