0

我正在尝试在 android 4.0.4 上启动我的无障碍服务,但无法启动。它在 2.3.3 上运行良好。但是更新版本没有运气。我已经尝试过这个建议。AccessibilityService 已启动,但在 JellyBean 上没有收到 AccessibilityEvents 这是我的代码。

公共类 NotificationService 扩展 AccessibilityService {

static final String TAG = "NotificationService";


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 onAccessibilityEvent(AccessibilityEvent event) {
    Toast.makeText(this, "Got event from " + event.getPackageName(), Toast.LENGTH_SHORT)
            .show();
    //Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
   // v.vibrate(new long[] { 0, 250, 250, 250, 250, 250, 250, 250, 250 }, -1);

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

  }

  @Override
  public void onInterrupt() {Log.v(TAG, "onInterrupt"); }

  @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.v(TAG, "onServiceConnected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        setServiceInfo(info);
    }

}

清单是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.watchservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.VIBRATE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.watchservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".NotificationService" android:enabled="true" 
            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/accessibilityservice" />

    </service>
    </application>

</manifest>

建议我,我哪里出错了?

4

1 回答 1

0

我错误地在清单中添加了一个元标记,这基本上是针对高于 ICS 的版本。从 android 4.0.4 应用程序中删除元标记后,它可以工作。

如果我在 Manifest 文件中有此标签,则它不起作用,正如建议的那样,它适用于较新版本的 android。

<meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" />
于 2013-11-11T07:27:54.327 回答