2016-11-26 160 views
1

我有一個擴展了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 
     } 
    } 

提前感謝!

回答

3

AccesibilityService是Service類的繼承類。因此,我們可以參考這個問題本: How to have Android Service communicate with Activity

最簡單的方式對你的問題:

1)調用startService()在活動的的onDestroy()方法:

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

這是最簡單的方法。確保你發送的數據是可配置的。另一種選擇是通過粘合劑 –