Android:绑定服务同时处理来自客户端

时间:2015-12-02 11:37:34

标签: android multithreading service messenger

这是我的问题: 为了简化我使用IntentService使用Messenger对象处理一对消息,第一条消息(msg.what == 1)启动10秒进程,第二条消息(msg.what == 2)启动5秒进程。这些消息由绑定到我的服务的第三方活动发送,并且正在等待对这些已发送消息的回复。

一切正常但如果消息1正在运行,则在发送消息2时,它将等待第一个进程完成处理。当我读到这个Intent Service的预期行为时(消息在外部线程中按顺序排队和处理)。

但是当前一个消息仍在运行时,有没有一个技巧可以对新发送的消息进行异步响应?(即当消息1仍在运行时获得对消息2的回复)我试过在我的handleMessage函数上使用thread和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();}

客户类:活动(第三方应用程序)

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();
            }
        }
    }

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

好的,考虑到pskink的链接(http://pastebin.com/L1m3NJTT

这是我尝试过的:我现在扩展了Service而不再使用IntentService。我的处理程序现在是

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

此方法必须在myRunnable方法中使用replyto方法获取传入消息(msg)

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();
    }
}

}

请注意,我仍然使用Service Interface onBind实现:

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

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

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