在Android手机主屏幕上检测触摸或轻扫

时间:2019-03-14 09:32:55

标签: android touch swipe accessibilityservice

我想检测手机主屏幕上的触摸或滑动。检测到后,我需要执行我的操作。我正在为此使用无障碍服务。但是无法检测到手机主屏幕上的触摸或滑动。谁能帮帮我吗。下面是我的代码

global_accessibility_service.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:accessibilityEventTypes="typeAllMask"
   android:accessibilityFeedbackType="feedbackAllMask"
   android:accessibilityFlags="flagDefault"
   android:canPerformGestures="true"
   android:canRequestTouchExplorationMode="true"
   android:canRetrieveWindowContent="true"
   android:description="@string/accessibility_service_description"
   android:notificationTimeout="100"
   tools:ignore="UnusedAttribute" />

服务等级

public class GlobalAccessibilityService extends AccessibilityService {
   FrameLayout mLayout;

   @SuppressLint("MissingPermission")
   @Override
   public void onCreate() {
   }

   @SuppressLint("InlinedApi")
   @Override
   protected void onServiceConnected() {
       super.onServiceConnected();
   }

   @SuppressLint("SwitchIntDef")
   @Override
   public void onAccessibilityEvent(AccessibilityEvent event) {
       final int eventType = event.getEventType();
       String eventText = null;
       switch (eventType) {
           case AccessibilityEvent.TYPE_VIEW_CLICKED:
               eventText = "Clicked: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_FOCUSED:
               eventText = "Focused: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED:
               eventText = "ACCESSIBILITY_FOCUS_CLEARED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED:
               eventText = "ACCESSIBILITY_FOCUS: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_CONTEXT_CLICKED:
               eventText = "CONTEXT_CLICKED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
               eventText = "HOVER_ENTER: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT:
               eventText = "HOVER_EXIT: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
               eventText = "LONG_CLICKED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_SCROLLED:
               eventText = "SCROLLED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_SELECTED:
               eventText = "SELECTED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
               eventText = "TEXT_CHANGED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
               eventText = "TEXT_SELECTION_CHANGED: ";
               break;

           case AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY:
               eventText = "TRAVERSED_AT_MOVEMENT_GRANULARITY: ";
               break;

           case AccessibilityEvent.TYPE_ANNOUNCEMENT:
               eventText = "ANNOUNCEMENT: ";
               break;

           case AccessibilityEvent.TYPES_ALL_MASK:
               eventText = "ALL_MASK: ";
               break;

           case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
               eventText = "WINDOW_STATE_CHANGED: ";
               break;

           case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
               eventText = "WINDOW_CONTENT_CHANGED: ";
               break;

           case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
               eventText = "TOUCH_INTERACTION_START: ";
               break;

           case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
               eventText = "TOUCH_INTERACTION_END: ";
               break;

           case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
               eventText = "TOUCH_EXPLORATION_GESTURE_START: ";
               break;

           case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
               eventText = "TOUCH_EXPLORATION_GESTURE_END: ";
               break;
       }

       eventText = eventText + event.getContentDescription();
   }

   @Override
   public void onInterrupt() {

   }
}

清单

<service
           android:name=".GlobalAccessibilityService"
           android:label="@string/accessibility_service_label"
           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/global_accessibility_service" />
       </service>

1 个答案:

答案 0 :(得分:1)

否,不可能。即使通过辅助功能服务,我们也无法在主屏幕上检测到触摸或滑动。