如何使用AIDL远程服务来处理不同模式的客户的并发请求? [英] How can I use AIDL remote service to deal with defferent clients' concurrent requests?

查看:447
本文介绍了如何使用AIDL远程服务来处理不同模式的客户的并发请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我书面方式一个插件,它定义一个远程服务,并为第三方开发者提供AIDL接口。

I'm writting a plug-in which defines a remote Service and provides a AIDL interface for 3rd party developers.

我如何使用这个远程服务来处理不同模式的客户的并发请求?

How can I use this remote service to deal with defferent clients' concurrent requests ?

这是该服务的apk activitys可以保持状态为每一个客户,当他们彼此,怎么办呢?之间进行切换

It is that service apk's activitys can keep status for each client, when they switched between each other, how to do it?

谢谢!

推荐答案

这可以通过使用HandlerThread与尺蠖它保持和服务从100个应用程序,无论收到的所有请求的实现。

This can be achieved using HandlerThread with Looper which maintains and service all the request no matter received from 100 applications.

有关这个AIDL回调接口也需要被添加为请求将通过这些回调来提交。

For this AIDL callback interface is also needs to be added as request will be furnished through these callbacks.

Server应用程序

IAidlService.aidl

interface IAidlService {
    void getStockInfo(IAidlCallback callback);
}

IAidlCallback.aidl

oneway interface IAidlCallback {
    void handleStockInfo(in Stock stockinfo);
}

Stock.aidl

parcelable Stock;

Stock.java

public class Stock implements Parcelable {

String stockName;

public String getStockName() {
    return stockName;
}

public void setStockName(String stockName) {
    this.stockName = stockName;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(stockName);
}

public static final Creator<Stock> CREATOR = new Parcelable.Creator<Stock>() {
    @Override
    public Stock createFromParcel(Parcel in) {
        return new Stock(in);
    }

    @Override
    public Stock[] newArray(int size) {
        return new Stock[size];
    }
};

public Stock(Parcel in) {
    this.stockName = in.readString();
}

public Stock() {}
}

AidlService.java

这是一个覆盖AIDL服务方法,并实现他们的主要服务类。它还处理请求与输出特定的应用程序要求它的回报。

This is main Service class which overrides the AIDL Service methods and implements them. It also handled the return of request with output to specific application requesting for it.

public class AidlService extends Service {

private static final int MSG_STOCK_INFO = 53;

private ArrayList<IAidlCallback> mRemoteCallbacks;

private ServiceHandler mHandler = null;

HandlerThread mHandlerThread = new HandlerThread("AidlServiceThread");

@Override
public void onCreate() {
    super.onCreate();

    mRemoteCallbacks = new ArrayList<>();
}

@Override
public IBinder onBind(Intent intent) {

    // Handler Thread handling all call back methods
    mHandlerThread.start();
    mHandler = new ServiceHandler(mHandlerThread.getLooper());

    return mBinder;
}

/**
 * Stub implementation for Remote service
 */
IAidlService.Stub mBinder = new IAidlService.Stub() {

    @Override
    public void getStockInfo(IAidlCallback callback) throws RemoteException {

        sendMsgToHandler(callback, MSG_STOCK_INFO);
    }
};

/**
 * Create handler message to be sent
 *
 * @param callback
 * @param flag
 */
void sendMsgToHandler(IAidlCallback callback, int flag) {

    mRemoteCallbacks.add(callback);

    Message message = mHandler.obtainMessage();
    message.arg1 = mRemoteCallbacks.size() - 1;

    message.what = flag;
    mHandler.sendMessage(message);
}

/**
 * Handler class sending result in callback to respective
 * application
 */
private class ServiceHandler extends Handler {
    int callbackIndex = 0;

    ServiceHandler(Looper looper) {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        callbackIndex = msg.arg1;

        switch (msg.what) {

            case MSG_STOCK_INFO:

                Stock stock = new Stock();
                stock.setStockName("Apple Inc");

                try {
                    mRemoteCallbacks.get(callbackIndex).handleStockInfo(stock);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
}

客户端应用程序

在任何客户端应用程序创建一个 ServiceConnection ,一旦绑定到服务,你只需要做出存根类IAidlCallback`并一起发送对象的 getStockInfo 调用。这样的:

In any client app create a ServiceConnection and once binded to service you simply need to make Stub class for 'IAidlCallback` and send object along with getStockInfo call. Like:

IAidlCallback.Stub callback = new IAidlCallback.Stub() {
    @Override
    public void handleStockInfo(Stock stockinfo) throws RemoteException {

        // do what ever you want with stock info :)
    }
};

希望它能帮助:)

Hope it helps :)

这篇关于如何使用AIDL远程服务来处理不同模式的客户的并发请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆