У меня есть два приложения - MyApplication и FirstApp.В MyApplicaton есть кнопка, по нажатию которой я перехожу на FirstApp.
Теперь я хочу прочитать содержимое FirstApp с помощью AccessibilityService.
Мой код выглядит так для 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.