ContextHub: ContextHubTransaction

public class ContextHubTransaction<T>

/*
* The response of the transaction.
*/
private ContextHubTransaction.Response<T> mResponse;

开始时忽略了这个类,以为只是定义了事件类型,它

1.describing a request sent to the Context Hub Service

2.listen处理返回结果Response,等待返回结果的方法

3.ContextHubManager APIs 参数

/**
 * A class describing a request sent to the Context Hub Service.
 *
 * This object is generated as a result of an asynchronous request sent to the Context Hub
 * through the ContextHubManager APIs. The caller can either retrieve the result
 * synchronously through a blocking call ({@link #waitForResponse(long, TimeUnit)}) or
 * asynchronously through a user-defined listener
 * ({@link #setOnCompleteListener(OnCompleteListener, Executor)} )}).
 *
 * @param <T> the type of the contents in the transaction response
 */

枚举类型的定义

听说是枚举类型太占内存,使用的类似宏替代的方法
    /**
     * Constants describing the type of a transaction through the Context Hub Service.
     * {@hide}
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = { "TYPE_" }, value = {
            TYPE_LOAD_NANOAPP,
            TYPE_UNLOAD_NANOAPP,
            TYPE_ENABLE_NANOAPP,
            TYPE_DISABLE_NANOAPP,
            TYPE_QUERY_NANOAPPS
    })
public @interface Type { }

    public static final int TYPE_LOAD_NANOAPP = 0;
    public static final int TYPE_UNLOAD_NANOAPP = 1;
    public static final int TYPE_ENABLE_NANOAPP = 2;
    public static final int TYPE_DISABLE_NANOAPP = 3;
    public static final int TYPE_QUERY_NANOAPPS = 4;

public @interface Result {}

使用方法

typeToString(@Type int type, boolean upperCase)
Response(@ContextHubTransaction.Result int result, R contents)

内部类:public static class Response<R>

模板参数<R>: 来着 ContextHubTransaction.Response<T>
/**
* A class describing the response for a ContextHubTransaction.
*
* @param <R> the type of the contents in the response
*/

    public static class Response<R> {
        /*
         * The result of the transaction.
         */
        @ContextHubTransaction.Result
        private int mResult;

        /*
         * The contents of the response from the Context Hub.
         */
        private R mContents;

        Response(@ContextHubTransaction.Result int result, R contents) {
            mResult = result;
            mContents = contents;
        }

        @ContextHubTransaction.Result
        public int getResult() {
            return mResult;
        }

        public R getContents() {
            return mContents;
        }
    }

内部接口类:public interface OnCompleteListener<L>

/**
* An interface describing the listener for a transaction completion.
*
* @param <L> the type of the contents in the transaction response
*/
    @FunctionalInterface
    public interface OnCompleteListener<L> {
        /**当传输完成时改函数被调用
         * The listener function to invoke when the transaction completes.
         * @param transaction: the transaction that this callback was attached to.
         * @param response:  the response of the transaction.
         */
        void onComplete(
                ContextHubTransaction<L> transaction, ContextHubTransaction.Response<L> response);
    }

向外提供的接口:设置listener and executor

public void setOnCompleteListener(
            @NonNull ContextHubTransaction.OnCompleteListener<T> listener,
            @NonNull @CallbackExecutor Executor executor) {
        synchronized (this) {
            mListener = listener;
            mExecutor = executor; //listener所在的线程
        }
}

public void setOnCompleteListener(//上面的特例,使用当前的主线程
            @NonNull ContextHubTransaction.OnCompleteListener<T> listener) {
        setOnCompleteListener(listener, new HandlerExecutor(Handler.getMain()));
}

向外提供的接口:setResponse

/**
* Sets the response of the transaction.
*
* This method should only be invoked by ContextHubManager as a result of a callback from
* the Context Hub Service indicating the response from a transaction. This method should not be
* invoked more than once.
*/
 void setResponse(ContextHubTransaction.Response<T> response) {
        synchronized (this) {
            mResponse = response;
            mIsResponseSet = true;

            mDoneSignal.countDown();
            if (mListener != null) {
                mExecutor.execute(() -> mListener.onComplete(this, mResponse));
            }
        }
}

如果没有setOnCompleteListener可以用WaitForResonse得到 mResponse


public ContextHubTransaction.Response<T> waitForResponse(
            long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
        boolean success = mDoneSignal.await(timeout, unit);

        if (!success) {
            throw new TimeoutException("Timed out while waiting for transaction");
        }

        return mResponse;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值