Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data back from Activity to AccessibilityService?

I have a class that extends AccessibilityService and when there is a certain event starts an activity.

The problem is that when the activity ends, it should send data back to 'AccessibilityService'. Does anyone have an idea on how to do that?

Example:

public class MyAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        if (event.getEventType()==AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED){
                Intent intent=new Intent(getApplicationContext(),DialogActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivity(intent);
                // String resul=set result When Activity is closed
        }
    }

Thanks in advance!

like image 319
Giuseppe Raddato Avatar asked Oct 23 '25 07:10

Giuseppe Raddato


1 Answers

AccesibilityService is an inherited class from Service class. So we can refer that question to this: How to have Android Service communicate with Activity

The easiest way for your question:

1) Call startService() in your Activity's onDestroy() method:

@Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(getApplicationContext(), MyAccessibilityService.class);
        intent.putExtra("data","yourData");
        startService(intent);
    }

2) Override your MyAccessibilityService's onStartCommand() method:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        String data="";
        if(intent.getExtras().containsKey("data"))
            data = intent.getStringExtra("data");
        return START_STICKY;
    }
like image 153
Sait Banazili Avatar answered Oct 24 '25 22:10

Sait Banazili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!