5

我正在编写一个插件,它定义了一个远程服务并为第 3 方开发人员提供了一个 AIDL 接口。

如何使用此远程服务来处理不同客户端的并发请求?

就是service apk的activity可以为每个client保持状态,当它们相互切换时,怎么做呢?

4

2 回答 2

13

这可以使用带有 Looper 的 HandlerThread 来实现,它维护和服务所有来自 100 个应用程序的请求。

由于请求将通过这些回调提供,因此还需要添加此 AIDL 回调接口。

服务器应用

IAidlService.aidl

interface IAidlService {
    void getStockInfo(IAidlCallback callback);
}

IAidlCallback.aidl

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

股票.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 服务方法并实现它们的主要服务类。它还处理请求的返回以及对请求它的特定应用程序的输出。

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”创建 Stub 类,并将对象与getStockInfo调用一起发送。喜欢:

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

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

希望能帮助到你 :)

于 2015-12-11T06:07:56.633 回答
0

与编写多线程服务器没有太大区别。您为每个客户端保留线程本地状态,并可能给他们某种会话 ID,以便您知道与谁交谈。

于 2012-12-19T09:21:14.430 回答