0

我有两个应用程序 - 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>

可访问性_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。

4

1 回答 1

2

您似乎正在尝试从您的活动中启动无障碍服务。你不能这样做。您必须从无障碍服务菜单启动无障碍服务。设置 > 通用 > 辅助功能。您还可以摆脱试图从您的活动中启动服务的代码。

于 2018-06-10T13:44:49.010 回答