启用辅助功能后,不会调用onServiceConnected

时间:2018-06-08 08:38:04

标签: android accessibility accessibilityservice android-accessibility accessibility-api

我有两个应用程序 - MyApplication和FirstApp。 MyApplicaton上有一个按钮,点击该按钮我将导航到FirstApp。

现在,我想使用AccessibilityService阅读FirstApp的内容。

对于MyApplication,我的代码是这样的 - MainActivity.java

package com.example.abc.myapplication;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this,MyAccessibilityService.class);
        startService(intent);
        mButton = (Button) findViewById(R.id.click_me);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc.firstapp");
      startActivity(launchIntent);
    }
}

MyAccessibilityService.java

package com.example.aayushi.myapplication;

public class MyAccessibilityService extends AccessibilityService {
    private AccessibilityServiceInfo info;

    @Override
    protected void onServiceConnected() {
        Log.i("service connected","service");
        info = new AccessibilityServiceInfo();
        info.eventTypes=AccessibilityEvent.TYPES_ALL_MASK;
        info.notificationTimeout=100;
        info.feedbackType=AccessibilityEvent.TYPES_ALL_MASK;
        info.packageNames = new String[]{"com.example.abc.firstapp"};
        setServiceInfo(info);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
       Log.i("onEvent----","yay");
       Log.i("event-----------", event.toString());
        //Log.i("source", event.ge)
    }

    @Override
    public void onInterrupt() {

    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abc.myapplication">
    <uses-feature android:name="android.hardware.fingerprint"/>
    <uses-permission android:name="android.permission.USE_FINGERPRINT"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service android:name=".MyAccessibilityService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
            >
            <meta-data android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_config"/>
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
        </service>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

accessibility_config.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:packageNames="com.example.abc.myapplication"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFlags="flagDefault"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    android:settingsActivity="com.example.abc.myapplication.MyAccessibilityService" />

问题是在设备上启用辅助功能后,onServiceConnected()根本就没有被调用。 为了测试,我使用的是Android 8.0.0,API 26。

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试从活动中启动辅助功能服务。你不能这样做。您必须从辅助功能服务菜单启动辅助功能服务。设置&gt;一般&gt;可访问性。您还可以摆脱尝试从您的活动中启动服务的代码。