2016-07-23 85 views
0

我嘗試在打開新活動或窗口時使用輔助功能服務來處理事件。但它不起作用。如何通過TYPE_WINDOW_STATE_CHANGED使用輔助功能服務

**首先,我實現無障礙服務類的樣子:

public class DemoAccessibilityService extends AccessibilityService { 
@Override 
public void onAccessibilityEvent(AccessibilityEvent event) { 
    System.out.println(" DemoAccessibilityService change= " + event.getAction()); 

} 

@Override 
public void onInterrupt() { 
} 

@Override 
protected void onServiceConnected() { 
    System.out.println("DemoAccessibilityService connected"); 
    super.onServiceConnected(); 
    AccessibilityServiceInfo config = new AccessibilityServiceInfo(); 
    config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED; 
    config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC; 

    if (Build.VERSION.SDK_INT >= 16) 
     config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS; 

    setServiceInfo(config); 
} 

@Override 
public void onDestroy() { 
    System.out.println("DemoAccessibilityService destroy"); 
    super.onDestroy(); 
} 

}

有2個選項來配置服務:(1)使用.xml文件,或(2)配置dinamically在源代碼上。我有demo_accessibility.xml配置文件:

<accessibility-service 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:accessibilityEventTypes="typeWindowStateChanged" 
android:accessibilityFeedbackType="feedbackGeneric" 
android:accessibilityFlags="flagIncludeNotImportantViews" 
tools:ignore="UnusedAttribute" 
android:packageNames="com.android.systemui" 

/>

**。然後,我宣佈它在AndroidManifest.xml這樣的:

<service android:name="demo.android.accessibility.DemoAccessibilityService" 
      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/demo_accessibility"/> 
    </service> 

建設中的設備後,我進入「設置」並打開「輔助功能」類別。 但它不起作用!我該怎麼辦?

回答

0

您必須在demo_accessibility.xml文件中添加android:canRetrieveWindowContent="true"