2017-06-07 184 views
0

我一直在尋找可用於Android O的輔助功能的新方法。我碰到了一個名爲getAccessibilityButtonController的新方法,我不確定它的功能和用途。我知道在Android O中有一個導航按鈕可用於無障礙服務。此可訪問性按鈕僅啓動可訪問性服務,還是可以在服務中具有其他功能,例如執行特定任務?我很好奇可用性的可訪問性和getAccessibilityButtonController方法。感謝您的時間。getAccessibilityButtonController Android輔助功能服務

回答

0

它可以做任何你想做的事情。從android accessibility doc開始,該按鈕允許您註冊一個具有onClicked方法的回調。如果啓用該按鈕並提供回調,則可以在該回調的上下文中執行任何您想要的操作。

請注意,如果您閱讀文檔,目前有一個示例在onCreate()中調用了getAccessibilityButtonController()。這是不正確的,因爲控制器在調用onServiceConnected之前無效。我修改了下面的示例以顯示應該起作用的內容。

private AccessibilityButtonController mAccessibilityButtonController; 
private AccessibilityButtonController 
     .AccessibilityButtonCallback mAccessibilityButtonCallback; 
private boolean mIsAccessibilityButtonAvailable; 

@Override 
protected void onServiceConnected() { 
    mAccessibilityButtonController = getAccessibilityButtonController(); 
    mIsAccessibilityButtonAvailable = 
      mAccessibilityButtonController.isAccessibilityButtonAvailable(); 

    if (!mIsAccessibilityButtonAvailable) { 
     return; 
    } 

    AccessibilityServiceInfo serviceInfo = getServiceInfo(); 
    serviceInfo.flags 
      |= AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON; 
    setServiceInfo(serviceInfo); 

    mAccessibilityButtonCallback = 
     new AccessibilityButtonController.AccessibilityButtonCallback() { 
      @Override 
      public void onClicked(AccessibilityButtonController controller) { 
       Log.d("MY_APP_TAG", "Accessibility button pressed!"); 

       // Add custom logic for a service to react to the 
       // accessibility button being pressed. 
      } 

      @Override 
      public void onAvailabilityChanged(
       AccessibilityButtonController controller, boolean available) { 
       if (controller.equals(mAccessibilityButtonController)) { 
        mIsAccessibilityButtonAvailable = available; 
       } 
      } 
     }; 

    if (mAccessibilityButtonCallback != null) { 
     mAccessibilityButtonController.registerAccessibilityButtonCallback(
       mAccessibilityButtonCallback, null); 
    } 
}