2015-12-02 103 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(); 
} 

事实是:我现在从未收到有关发送消息的活动的任何答案。我错过了什么 ?

相关问题