我有一个扩展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是从服务类继承的类。所以我们可以把这个问题提出来:
How to have Android Service communicate with Activity
最简单的提问方式:
1)在活动的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;
}