2013-12-15 104 views
1

我正在尋找我的問題的答案,並且我沒有在任何地方找到答案。AccesibilityService與活動之間的溝通

我有一個AccesibilityService正在運行,我想從它發送信息到MainActivity。

我一直試圖用Messenger類來做,但我不能。

任何人都可以幫助我做到這一點嗎?

我已經把這段代碼在MainActivity:

class IncomingHandler extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case NoficationService.MSG_ENCENDER: 
      // Envía "1". 
      String message = "1"; 
      mApp1.sendData(message); 
      break; 
     default: 
      super.handleMessage(msg); 
     } 
    } 
} 

在MainActivity我有這樣的代碼:

try { 
      Message msg = Message.obtain(null, NoficationService.MSG_ENCENDER, 0, 0); 
      msg.replyTo = mMessenger; 
      mService.send(msg); 
      Log.d(tag, "Enviado Msg"); 
     } catch (RemoteException e) { 
      Log.d(tag, "Excepción Msg"); 
     } 

等待您的幫助。先謝謝你!

回答

0

嘗試使用廣播:

public final static String BROADCAST_ACTION = "com.packegeName"; 

1)在活動註冊廣播:

BroadcastReceiver br = new BroadcastReceiver() { 

public void onReceive(Context context, Intent intent) { 
     // proccess messages 
} 
} 


IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION); 
registerReceiver(br, intFilt); 

2)從AccesibilityService

Intent intent = new Intent(MainActivity.BROADCAST_ACTION); 
intent.putExtra(MainActivity.PARAM_STATUS, MainActivity.STATUS_FINISH); 
intent.putExtra(MainActivity.PARAM_RESULT, time * 100); 
sendBroadcast(intent); 
+1

它可以發送廣播。非常感謝! – ChristLarsen