2015-12-02 42 views
0

這是我的問題: 爲了簡化我使用IntentService處理一對使用Messenger對象的消息,第一條消息(msg.what == 1)啓動一個10秒進程,第二個(msg.what == 2)啓動一個5秒的進程。這些消息由第三方Activity發送,該Activity綁定到我的服務,並且正在等待回覆這些發送的消息。Android:綁定服務同時處理來自客戶端的多個消息

一切工作正常,但如果消息1正在運行,發送消息2時,它將等待,直到第一個過程完成處理。當我讀到這個意向服務的預期行爲(消息在一個外部線程中按順序排隊和處理)。

但是,當前一個仍在運行時,獲得對新發送消息的異步響應有一個竅門嗎?(即得到消息2時消息1仍在進行回覆)我試圖用我的handleMessage功能的線程和的AsyncTask沒有成功(我甚至沒有得到在活動任何回答了我的請求)

服務類:處理器

class IncomingHanlder extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     try { 
      switch (msg.what) { 

       case 1: 
        //Process taking 10 sec 
        //... 

        //Reply to client 
        Message resp = Message.obtain(null, msg.what); 
        Bundle bResp = new Bundle(); 
        bResp.putBoolean("com.xxx.msg1", true); 
        resp.setData(bResp); 
        msg.replyTo.send(resp); 
        break; 

       case 2: 
        //Process taking 5 sec 
        //... 

        //Reply to client 
        Message resp = Message.obtain(null, msg.what); 
        Bundle bResp = new Bundle(); 
        bResp.putBoolean("com.xxx.msg2", true); 
        resp.setData(bResp); 
        msg.replyTo.send(resp); 
        break; 

       default: 
        super.handleMessage(msg); 
      } 
     } 
      catch (RemoteException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

private Messenger msg = new Messenger(new IncomingHanlder()); 

@Override 
public IBinder onBind(Intent arg0) {return msg.getBinder();} 

Client類:活動(第三方應用程序)

ServiceConnection sConn = new ServiceConnection() { 

      @Override 
      public void onServiceDisconnected(ComponentName name) { 
       messenger = null; 
      } 

      @Override 
      public void onServiceConnected(ComponentName name, IBinder service) { 
       // We are connected to the service 
       messenger = new Messenger(service); 
      } 
     }; 

     // We bind to the service 
     Intent i = new Intent("com.xxx.myserviceboundpackage"); 
     try { 
      //If service is always launched 
      stopService(i); 
     } 
     catch (Exception e){} 

     //Bind to service 
     bindService(i, sConn, 
       Context.BIND_AUTO_CREATE); 

在客戶端:從點擊一個按鈕(例如消息1)發送消息

   try { 
       Message msg = Message 
         .obtain(null, 1); 

       msg.replyTo = new Messenger(new ResponseHandler());; 

       try { 
        messenger.send(msg); 
       } catch (RemoteException e) { 

       } 
      } 
      catch (Exception e) 
      { 
       e.printstacktrace(); 
      } 

在客戶端:處理來自服務答覆消息以

class ResponseHanlder extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     try { 
      switch (msg.what) { 

       //Response to each request 
       case 1: 
        //Process which have taken 10 sec 

        Bundle bundle = msg.getData(); 
        Boolean myResp = bundle.getBoolean("com.xxx.msg1"); 
        if (myResp) {//do something} 
        break; 

       case 2: 
        //Process which have taken 5 sec 

        Bundle bundle = msg.getData(); 
        Boolean myResp = bundle.getBoolean("com.xxx.msg2"); 
        if (myResp) {//do something} 
        break; 

       default: 
        super.handleMessage(msg); 
      } 
     } 
      catch (RemoteException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

感謝建議。

+0

實際上你想達到什麼目的? – pskink

+0

Juste想象我發送消息1,並立即從我的活動中發送消息2。消息2不會立即處理,而是排隊。所以我得到它在10秒後(處理消息1)+ 5秒(處理已經排隊的消息2)的響應。我希望僅在5秒後得到我對消息2的響應(無需等待消息1過程完成) – Valentin

+0

由於您正在使用爲所有請求使用一個後臺線程的IntentService,因此如果要處理您的請求消息立即使用'「一條消息 - 一條線程」方法 – pskink

回答

0

好吧考慮pskink的鏈接(http://pastebin.com/L1m3NJTT

這裏是我的嘗試:我現在已經擴展服務並沒有更多IntentService。而我的處理程序,現在是

class IncomingHanlder extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     pool.submit(new MyRunnable(msg)); 
     super.handleMessage(msg); 
    } 
} 

這種方法有可能會按順序傳入消息(MSG)使用的replyTo方法是允許的,在這

public class MyRunnable implements Runnable { 
private final Message _message; 
public MyRunnable(final Message message) { 
    this._message = message; 
} 


@Override 
public void run() { 
try { 
    switch (this._message.what) { 

     case 1: 
      //Process taking 10 sec 
      //... 

      //Reply to client 
      Message resp = Message.obtain(null, this._message.what); 
      Bundle bResp = new Bundle(); 
      bResp.putBoolean("com.xxx.msg1", true); 
      resp.setData(bResp); 
      this._message.replyTo.send(resp); 
      break; 

     case 2: 
      //Process taking 5 sec 
      //... 

      //Reply to client 
      Message resp = Message.obtain(null, this._message.what); 
      Bundle bResp = new Bundle(); 
      bResp.putBoolean("com.xxx.msg2", true); 
      resp.setData(bResp); 
      this._message.replyTo.send(resp); 
      break; 

     default: 

    } 
} 
    catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 

}

注myRunnable方法我仍然使用服務接口onBind實現:

private Messenger msg = new Messenger(new ConvertHanlder()); 

    @Override 
public IBinder onBind(Intent arg0) { 
    return msg.getBinder(); 
} 

事實是:我現在從未收到有關發送消息的活動的任何答案。我錯過了什麼 ?

相關問題