2015-05-26 61 views
0

我試圖獲得關於輔助功能事件的一些教程。我找到了一個輔助教程。這是很好的&我在eclipse &中實現在我的平板電腦5.1.1運行,nexus。onAccessibityEvent在Android中未調用

編碼。

MyAccessibilityService.java

public class MyAccessibilityService extends AccessibilityService { 
     public static final String TAG = "volumeMaster"; 
     @Override 
     public void onAccessibilityEvent(AccessibilityEvent event) 
     { 
      Log.v(TAG, "***** onAccessibilityEvent"); 
      Toast.makeText(getApplicationContext(), "Got event from: " + event.getPackageName(), Toast.LENGTH_LONG).show(); 
      String s=String.valueOf(event.getEventType()); 
      Toast.makeText(MyAccessibilityService.this, s, Toast.LENGTH_LONG).show(); 
      Toast.makeText(MyAccessibilityService.this, getEventText(event), Toast.LENGTH_LONG).show(); 
      Log.v(TAG, String.format(
        "onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s", 
        getEventType(event), event.getClassName(), event.getPackageName(), 
        event.getEventTime(), getEventText(event))); 
     } ` 
     private String getEventType(AccessibilityEvent event) { 
      switch (event.getEventType()) { 
       case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: 
        return "TYPE_NOTIFICATION_STATE_CHANGED"; 
       case AccessibilityEvent.TYPE_VIEW_CLICKED: 
        return "TYPE_VIEW_CLICKED"; 
       case AccessibilityEvent.TYPE_VIEW_FOCUSED: 
        return "TYPE_VIEW_FOCUSED"; 
       case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED: 
        return "TYPE_VIEW_LONG_CLICKED"; 
       case AccessibilityEvent.TYPE_VIEW_SELECTED: 
        return "TYPE_VIEW_SELECTED"; 
       case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: 
        return "TYPE_WINDOW_STATE_CHANGED"; 
       case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED: 
        return "TYPE_VIEW_TEXT_CHANGED"; 
      } 
      return "default"; 
     } 
     private String getEventText(AccessibilityEvent event) { 
      StringBuilder sb = new StringBuilder(); 
      for (CharSequence s : event.getText()) { 
       sb.append(s); 
      } 
      return sb.toString(); 
      } 
     @Override 
     public void onInterrupt() 
     { 
      Log.v(TAG, "***** onInterrupt"); 
     } 
     @Override 
     public void onServiceConnected() 
     { 
      Log.v(TAG, "***** onServiceConnected");  
      AccessibilityServiceInfo info = new AccessibilityServiceInfo(); 
      info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; 
      info.notificationTimeout = 100; 
      info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK; 
      setServiceInfo(info); 
     } 

}

在此代碼如果刪除getEventType & getEventtext起作用它示出了作爲64東西事件類型。如果我添加這兩個函數我不能得到事件操作。我嘗試了幾次,我打開我的應用程序來聽取可訪問性點擊事件。

我的清單文件低於 AndroidManifest.xml中

 <application 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/app_name" 
      android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> 
      <service android:name=".MyAccessibilityService" android:label="@string/app_name" android:enabled="true" android:exported="false"> 
      <intent-filter> 
       <action android:name="android.accessibilityservice.AccessibilityService" /> 
      </intent-filter> 
      <meta-data 
     android:name="android.accessibilityservice" 
     android:resource="@xml/accessibility" /> 
     </service> 
      <activity 
       android:name=".Toast2Activity" 
       android:label="@string/app_name" > 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 
     </application> 

任何一個可以幫我找出我在此代碼所做的缺陷。提前致謝。

回答

1

前幾天剛剛在Android 4.4和5.0上試用過,它工作正常。 如果您已經在config-xml(「@ xml/accessibility」)中定義了所有內容,則無需在onServiceConnected()中設置AccessibilityServiceInfo。在AndroidManifest.xml中,你在應用程序中設置了「android:permission」,我不知道它是否可以。

public class MyAccessibilitySvc extends AccessibilityService { 
    private static final String TAG = "MyAccessibilitySvc"; 

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

    @Override 
    public void onAccessibilityEvent(AccessibilityEvent event) { 
     AccessibilityNodeInfo nodeInfo = event.getSource(); 
     if (null == nodeInfo) return; 
     Log.i(TAG, "onAccessibilityEvent: event=" + event); 
    } 

    @Override 
    public void onInterrupt() { 

    } 
} 

在AndroidManifest.xml:

<service android:name=".MyAccessibilitySvc" 
     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/accessibilitysvc_config"/> 
    </service> 

xml配置:

<?xml version="1.0" encoding="utf-8"?> 
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" 
    android:accessibilityEventTypes="typeViewFocused|typeViewClicked" 
    android:accessibilityFeedbackType="feedbackAllMask" 
    android:notificationTimeout="100" 
    android:canRetrieveWindowContent="true" 
    /> 
+0

雅其工作......但是當我在Chrome中點擊網頁按鈕,我不能得到的作用onAccessibility事件。因爲那裏有任何其他的輔助事件嗎?如果有請回復我... – user3546693

+1

我現在無法測試它,但我記得我的演示應用程序確實在點擊網頁editText時得到了一些事件。 @ user3546693 – wrkwrk

+0

謝謝你的國王回覆。什麼事件,如果是的話,請張貼它。 @wrkwrk – user3546693

相關問題