4

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

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

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>

语音配置.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"/>

语音服务.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);
  }
}
4

1 回答 1

0

答案是:在voice_config.xml

android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"应修改为您自己的活动名称。

于 2017-08-15T02:54:59.437 回答