问题描述
我有一个扩展 AccessibilityService 的类,当有某个事件时会启动一个活动.
I have a class that extends AccessibilityService and when there is a certain event starts an activity.
问题是当活动结束时,它应该将数据发送回AccessibilityService".有没有人知道如何做到这一点?
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?
示例:
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 类继承的类.所以我们可以参考这个问题:如何让 Android Service 与 Activity 通信
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) 在 Activity 的 onDestroy() 方法中调用 startService():
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) 覆盖您的 MyAccessibilityService 的 onStartCommand() 方法:
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;
}
这篇关于如何将数据从 Activity 发送回 AccessibilityService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!