启用我的AccessibilityService后未调用onServiceConnected

时间:2017-08-14 16:23:49

标签: java android service accessibility onserviceconnected

打开我的应用后,它会跳转到辅助功能设置。但是,在我打开我的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>

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

1 个答案:

答案 0 :(得分:0)

这就是答案: 在sed -e '/<%/ # When we match the start pattern, '<%', execute the following command block { 1!b; # If we're not on the first line when we match the start pattern, exit :x; # Define label 'x' $!N; # If not at the end of file, capture the current line /%>/!bx; # If we're not at the end pattern, '%>', go to label 'x' s/<%.*%>// # After reaching the end pattern, replace all matching text in the captured lines with nothing }' -i "file"

voice_config.xml应修改为您自己的活动名称。