打开我的应用程序后,它会跳转到辅助功能设置。但是在打开 onServiceConnected(称为 AccessibilityService )后,不会调用 VoiceService

你能告诉我我该怎么办吗?尽管我在 XML 中启用了 VoiceService,但它似乎无法启动。或者我需要以不同的方式将 MainActivityVoiceService 绑定(bind)?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.hearexpr">
<uses-permission
    android:enabled="true"
    android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name=".VoiceService"
        android:enabled="true"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="@string/service_name">
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/voice_config"/>
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>

    </service>

</application>
</manifest>

voice_config.xml
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:description="@string/service_description"
  android:packageNames="com.example.wang.hearexpr"
  android:accessibilityEventTypes="typeAllMask"
  android:accessibilityFlags="flagDefault"
  android:accessibilityFeedbackType="feedbackSpoken"
  android:notificationTimeout="100"
  android:canRetrieveWindowContent="true"
  android:canRequestTouchExplorationMode="true"
  android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>

VoiceService.java
package com.example.wang.hearexpr;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

public class VoiceService extends AccessibilityService {
  private static final String TAG = "hear the voice";
  @Override
  public void onServiceConnected(){
      super.onServiceConnected();
      Log.d(TAG, "Service Connected");
  }

  @Override
  public void onAccessibilityEvent(AccessibilityEvent event) {
      Log.d(TAG,"onAccessibilityEvent: "+ event.toString());
  }
  @Override
  public void onInterrupt() {
      Log.d(TAG, "Interrupted");
  }
  @Override
  public void onDestroy(){
      Log.d(TAG, "Destroyed");
  }
}

MainActivity.java
package com.example.wang.hearexpr;

import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
  private static final Intent sSettingsIntent =
        new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      startActivityForResult(sSettingsIntent,0);
  }
}

最佳答案

这是答案:
voice_config.xml
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" 应修改为您自己的 Activity 名称。

关于java - 启用我的 AccessibilityService 后未调用 onServiceConnected,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45678878/

10-08 20:37