其他分享
首页 > 其他分享> > android-如何将数据从活动发送回AccessibilityService?

android-如何将数据从活动发送回AccessibilityService?

作者:互联网

我有一个扩展AccessibilityService的类,当发生某个事件时,将启动一个活动.

问题在于活动结束时,应将数据发送回“ AccessibilityService”.有谁知道如何做到这一点?

例:

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
        }
    }

提前致谢!

解决方法:

AccesibilityService是Service类的继承类.因此,我们可以将这个问题提到此问题:
How to have Android Service communicate with Activity

您问题的最简单方法:

1)在Activity的onDestroy()方法中调用startService():

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

2)覆盖MyAccessibilityService的onStartCommand()方法:

 @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;
    }

标签:android-intent,accessibilityservice,android
来源: https://codeday.me/bug/20191026/1936873.html