仿EasyTouch实现一键返回功能

来源:互联网 发布:手机贴膜品牌 知乎 编辑:程序博客网 时间:2024/06/14 10:27

项目中有个类似EasyTouch点击返回键,实现返回的需求,参考了网上的一个例子点击打开链接。

首先需要用一个叫AccessibilityService类,这个服务不需要你在Activitu里面去开启,是属于系统级别的辅助服务,需要手动在设置里面开启,和我们平时使用的service不同。

定义一个BackService 继承AccessibilityService

package com.example.windowmanagertest;import android.accessibilityservice.AccessibilityService;import android.accessibilityservice.AccessibilityServiceInfo;import android.util.Log;import android.view.accessibility.AccessibilityEvent;import android.view.accessibility.AccessibilityNodeInfo;public class BackService extends AccessibilityService {private static BackService context;@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {Log.i("dly", "onAccessibilityEvent");context = this;}@Overridepublic void onInterrupt() {// TODO Auto-generated method stub}@Overrideprotected void onServiceConnected() {Log.i("dly", "onServiceConnected");context = this;super.onServiceConnected();}@Overridepublic void onCreate() {Log.i("dly", "onCreate");context = this;super.onCreate();}public static void back() {context.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);}}


2.然后在Manifest中去注册这个服务

<service android:name="com.example.windowmanagertest.BackService"             android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">            <intent-filter>                <action android:name="android.accessibilityservice.AccessibilityService" />            </intent-filter>            <meta-data                android:name="android.accessibilityservice"                android:resource="@xml/taskbackconfig" />        </service>

3.在res下面新建一个xml文件夹,里面新建taskbackconfig.xml,对应清单文件中android:resource

<?xml version="1.0" encoding="utf-8"?><!--  Copyright (C) 2011 The Android Open Source Project  Licensed under the Apache License, Version 2.0 (the "License");  you may not use this file except in compliance with the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and  limitations under the License. --><!--这个就是给我们的AccessibilityService 设置属性的,当然你也可以在代码里 的 connnect函数里 手动设置。可以向下兼容。accessibilityFeedbackType 这个属性如果不设置的话 我们那个onAccessibilityEvent 这个回调函数 根本回调不了 所以这里要注意packageNames 这个属性 就是捕获什么app的行为的,比如我这里写的包名是packageinstaller 那就肯定只能捕获安装器的 事件了有的rom 安装器可能不是这个包名 那你就要进行特殊设置了,此外这个属性你如果什么都不写 就意味着 你可以捕获所有手机的动作如果你要做流氓软件的话 可以packageNames 里面什么都不写。。。甚至可以操作支付宝 给你打钱。。。如果你知道用户密码的话。当然你如果真这么做了 相信捕获一次用户输入密码的行为 也是很容易的。。细思极恐 我就不往下深入了。。。description 这个就是对你那个申请服务的时候说明了,可以写的煽情一点 让用户打开这个服务的可能性更高一点。。。--><accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"    android:accessibilityEventTypes="typeAllMask"    android:notificationTimeout="100"    android:packageNames="com.android.packageinstaller"    android:accessibilityFeedbackType="feedbackSpoken"    android:canRetrieveWindowContent="true"    android:description="@string/action_settings" />

4.在你点击悬浮窗的时候调用一下BackService里面的back()方法就行了

BackService.back();
5.运行程序后,要在设置里面的辅助功能里面开启服务!


0 0
原创粉丝点击