2012-02-06 113 views
2

在我的應用程序中,我必須捕獲新的傳入電子郵件按摩通知,並在描述事件的屏幕上播放視頻。 我正在使用onAccessibilityEvent(AccessibilityEvent event)來偵聽生成的通知。 我也用下面的代碼init的我AccessibilityService:onAccessibilityEvent(AccessibilityEvent事件)不攔截通知

AccessibilityServiceInfo localAccessibilityServiceInfo = new AccessibilityServiceInfo(); 
    localAccessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK; // AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; 
    localAccessibilityServiceInfo.notificationTimeout = EVENT_NOTIFICATION_TIMEOUT_MILLIS; 
    localAccessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_AUDIBLE; // AccessibilityServiceInfo.FEEDBACK_VISUAL; 
    localAccessibilityServiceInfo.flags &= AccessibilityServiceInfo.DEFAULT; 
    setServiceInfo(localAccessibilityServiceInfo); 

但事件從未到達。 如何解決? 謝謝, Eyal。

回答

2

我有類似的情況,這部作品在我的應用程序:

import android.accessibilityservice.AccessibilityService; 
import android.accessibilityservice.AccessibilityServiceInfo; 
import android.util.Log; 
import android.view.accessibility.AccessibilityEvent; 
import android.widget.Toast; 

public class NotificationAccessibilityService 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(); 
    } 

    @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); 

    } 
} 

在我的清單: Accessibility Service from StackOverflow

<service android:name=".accessibility.NotificationAccessibilityService" android:label="@string/app_name" android:enabled="true" android:exported="false"> 
     <intent-filter> 
      <action android:name="android.accessibilityservice.AccessibilityService" /> 
     </intent-filter> 

</service> 

我的解決方案的部分從何而來