I have created an android Service
, which extends an AccessibilityService
. From my Activity
, I would like to bind to that Service
. Because I need to send information from Service
to Activity
. I need example code please. I have searched google and have not found anything similar, someone could put a link to an example please.
While binding with an AccessibilityService from an Activity is possible, you'll only be able to pass AccessibilityEvents into the service. This is because AccessibilityService.onBind() is declared final, which means you won't be able to add any new methods to the binder.
There are, however, several alternative solutions.
If your activity and service are running within the same process (this is typically true if they are in the same APK), then you can communicate with a static instance of your accessibility service. Here is an example of how that would work:
MyAccessibilityService.java:
private static MyAccessibilityService sSharedInstance;
protected void onServiceConnected() {
. . .
sSharedInstance = this;
}
public boolean onUnbind(Intent intent) {
sSharedInstance = null;
. . .
}
public static MyAccessibilityService getSharedInstance() {
return sSharedInstance;
}
MyActivity.java
protected void onCreate() {
. . .
mAccessibilityService = MyAccessibilityService.getSharedInstance();
if (mAccessibilityService != null) {
// The service is running and connected.
mAccessibilityService.doSomething();
}
}
If your service and activity are in separate processes (e.g. separate APKs), then you still have a few options:
If you need elaboration on either of those, leave a comment.
Maybe there is another way to communicate whith each other:
At AccessibilityService
Child Class:
Handle the target Activity's:
PackageName AND
ActivityName AND
specific EventType(eg:AccessibilityEvent.TYPE_VIEW_CLICKED
)
@Override
public void onAccessibilityEvent(final AccessibilityEvent event)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED)
if (AccessibilityUtils.findNodeInfoByViewTypeAndText(event.getSource(), "android.widget.ToggleButton", TextUtils.getString(R.string.lable_disabled)) != null)
getAccessibilityService().disableSelf();
}
At target Activity
:just invoke the specific EventType
invoke by user OR
invoke by code
btnEnableService.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(final View v)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !btnEnableService.isChecked())
{
btnEnableService.setChecked(false);
txvEnabledState.setText(btnEnableService.isChecked() ? R.string.lable_service_status_enabled : R.string.lable_service_status_closed);
AppState.instance().setIsEnable(btnEnableService.isChecked());
}
else
{
openAccessibilitySettings();
btnEnableService.setChecked(!btnEnableService.isChecked());
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With