当用户进入我的应用时,我想听听可访问性手势。这意味着当我的应用程序位于前台时,onGusture应该调用,但是无论我的应用程序是否位于前台,onGesture总是会调用。我尝试在Java和XML中设置程序包名称,但是它不起作用。请看看这个告诉我我做错了
MyAccessibilityService.java
public class MyAccessibilityService extends AccessibilityService {
private String TAG="MyAccessibilityServiceTAG";
@Override
protected boolean onGesture(int gestured) {//This method always called, I want this method only called when my App in foreground
Log.d(TAG,"onGesture:"+gestureId);
Toast.makeText(getApplicationContext(),"Gesture: "+gestureId,Toast.LENGTH_SHORT).show();
return super.onGesture(gestureId);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
System.out.println("Event Occurred");
Log.d(TAG, "onAccessibilityEvent: event=" + event);
AccessibilityNodeInfo nodeInfo = event.getSource();
if (null == nodeInfo) return;
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo info = getServiceInfo();
// your other assignments
info.packageNames = new String[]{"com.vorail.ttsdemo"};
setServiceInfo(info);
}
@Override
public void onInterrupt() {
Log.d(TAG,"Accessibility Interrupted" );
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"Service Destroyed");
}
}
accessibility_service_config.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_name"
android:accessibilityEventTypes="typeTouchExplorationGestureStart"
android:canRequestFilterKeyEvents="false"
android:accessibilityFeedbackType="feedbackSpoken"
android:packageNames="com.vorail.ttsdemo"
android:notificationTimeout="100"
android:settingsActivity="com.vorail.ttsdemo.MainActivity"
android:accessibilityFlags="flagDefault|flagRequestTouchExplorationMode"
android:canRequestTouchExplorationMode="true"
/>
答案 0 :(得分:1)
通过将android:packageNames
设置为特定于您的应用的内容,您将过滤传入的事件:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewClicked|typeViewFocused|typeTouchInteractionStart|typeTouchInteractionEnd|typeTouchExplorationGestureStart|typeTouchExplorationGestureEnd"
android:packageNames="com.app.yourapplication"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canPerformGestures="true"
android:canRetrieveWindowContent="true" />