AsyncQueryHandler上的异常处理

时间:2014-03-10 08:16:50

标签: android exception-handling

使用Android的AsyncQueryHandler时,有没有简单的方法来处理异步插入引发的异常,还是需要编写我自己的该类版本?

1 个答案:

答案 0 :(得分:5)

围绕AsyncQueryHandler来源,很明显我可以扩展它以满足我的需求而不会有太多的痛苦。由于“AsyncQueryHandler异常处理”在谷歌上没有给你太多,所以我在下面附上了我的源代码,以防其他任何人发现自己处于相同的情况。

基本上它的作用是,它扩展AsyncQueryHandler的内部工作线程并在try-catch块中将调用包装到该线程的super.handleMessage,然后将任何捕获的异常发送回主消息中的线程,它被传递到onError回调。默认onError只是重新抛出异常。

/**
 * Thin wrapper around @link {@link AsyncQueryHandler} that provides a callback to be invoked 
 * if the asynchronous operation caused an exception.
 */
public class SturdyAsyncQueryHandler extends AsyncQueryHandler {
    public SturdyAsyncQueryHandler(ContentResolver cr) {
        super(cr);
    }

    /**
     * Thin wrapper around {@link AsyncQueryHandler.WorkerHandler} that catches any <code>RuntimeException</code>
     * thrown and passes them back in a reply message. The exception that occurred is
     * in the <code>result</code> field.
     */
    protected class SturdyWorkerHandler extends AsyncQueryHandler.WorkerHandler {
        public SturdyWorkerHandler(Looper looper) {
            super(looper);
        }
        @Override
        public void handleMessage(Message msg) {
            try {
                super.handleMessage(msg);
            } catch (RuntimeException x) {
                // Pass the exception back to the calling thread (will be in args.result)
                WorkerArgs args = (WorkerArgs) msg.obj;
                Message reply = args.handler.obtainMessage(msg.what);
                args.result = x;
                reply.obj = args;
                reply.arg1 = msg.arg1;
                reply.sendToTarget();
            }
        }
    }

    @Override
    protected Handler createHandler(Looper looper) {
        return new SturdyWorkerHandler(looper);
    }

    /**
     * Called when a runtime exception occurred during the asynchronous operation. 
     * <p>
     * The default re-throws the exception
     * @param token - The token that was passed into the operation
     * @param cookie - The cookie that was passed into the operation
     * @param error - The <code>RuntimeException</code> that was thrown during 
     * the operation
     */
    public void onError(int token, Object cookie, RuntimeException error) {
        throw error;
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.obj instanceof WorkerArgs) {
            WorkerArgs args = (WorkerArgs) msg.obj;
            if (args.result instanceof RuntimeException) {
                onError(msg.what, args.cookie, (RuntimeException) args.result);
                return;
            }
        }
        super.handleMessage(msg);
    }
}