AsyncQueryHandler学习

来源:互联网 发布:流行的骂人网络用语 编辑:程序博客网 时间:2024/05/20 16:09
原文地址:AsyncQueryHandler学习作者:雨点点
学习这个类之前,需要先理解几个知识点:
1. Handler与Thread,Looper的关系
2. HandlerThread的作用
3. ThreadLocal类的作用
 
Handler主要是用来发送(sendMessage)和处理消息(handleMessage),但是发送了消息后,消息是怎么传递的呢?这就是Looper(消息泵)的作用了,每个Handler中都会有一个Looper对象,如果在创建Handler的时候不指定,系统就会默认将当前线程的Looper绑定到Handler上,Looper对象中维护者一个消息队列,Hander发送的消息都会存储到这个消息队列中,Looper不断的遍历这个消息队列,取出消息,交给handleMessage方法处理。Looper属于哪个线程,hadleMessage方法就会在那个线程中执行。

HandlerThread继承Thread,不但能提供异步处理,Handler处理消息的方法也会在这个线程中执行,他最要的作用就是提供了一个新线程。API解释如下:
Handy class for starting a new thread that has a looper.

ThreadLocal类主要是用来多个模块共享变量用的,但是不同线程之间的变量的值却不相同。即同一线程内,不同模块获取的对象(ThreadLocal类的实例)是同一个。

AsyncQueryHandler的工作机制是什么?
     
AsyncQueryHandler继承了Handler对象,但是他提供的构造方法中却没有Looper参数,也就是说他和他所在的当前线程绑定。
AsyncQueryHandler源码:
    finalWeakReference<ContentResolver>mResolver;
    privatestatic Looper sLooper = null;
    privateHandlermWorkerThreadHandler;  
    publicAsyncQueryHandler(ContentResolver cr) {
       super();
       mResolver = newWeakReference<ContentResolver>(cr);//ContentResolver的弱引用,操作相关数据
       //允许访问控制,同步执行
       synchronized (AsyncQueryHandler.class) {
           if (sLooper == null) {
               //提供一个新线程
               HandlerThread thread = new HandlerThread("AsyncQueryWorker");
               thread.start();
               //start()方法必须被调用,否则通过getLooper方法得到的looper对象是空的。通过调用start方法,就会去执行该线程的run方法,
               sLooper = thread.getLooper();
           }
       }
       mWorkerThreadHandler = createHandler(sLooper);
    }
    protectedHandler createHandler(Looper looper) {
       return new WorkerHandler(looper);
    }

AsyncQueryHandler内部有一个Hhandler对象,叫mWorkerHandler,他和一个HandlerThread绑定,mWorkerHandler负责将打包好的消息发送,并且处理,并将结果作为消息发送给AsyncQueryHandler。他是怎么发送的?AsyncQueryHandler内部有一个WorkerArgs完美类,他封装了startAsyncQuery等方法的参数,并且通过这行代码(红色部分)
    public voidstartQuery(int token, Object cookie, Uri uri,
           String[] projection, String selection, String[]selectionArgs,
           String orderBy) {
       // Use the token as what so cancelOperations worksproperly
       Message msg =mWorkerThreadHandler.obtainMessage(token);
       msg.arg1 = EVENT_ARG_QUERY;
       WorkerArgs args = newWorkerArgs();
       args.handler = this;
       args.uri = uri;
       args.projection = projection;
       args.selection = selection;
       args.selectionArgs = selectionArgs;
       args.orderBy = orderBy;
       args.cookie = cookie;
       msg.obj =args;

       mWorkerThreadHandler.sendMessage(msg);
   }
将当前Handler封装进去,发送到HandlerThread中去,HandlerThread绑定的mWorkerHandler,mWorkerHandler负责将打包好的消息发送,并且处理完消息得到结果后,args.handler将结构发送给自己进行处理。(这就是线程间的通信了)。mWorkerHandler和一个子线程绑定(HanderThread),能够处理比较耗时的操作,AsyncQueryHandler提供异步处理。
   
protected class WorkerHandler extends Handler {
       public WorkerHandler(Looper looper) {
           super(looper);
       }

       @Override
       public void handleMessage(Message msg) {
           final ContentResolver resolver = mResolver.get();
           if (resolver == null) return;

           WorkerArgs args = (WorkerArgs) msg.obj;

           int token = msg.what;
           int event = msg.arg1;

           switch (event) {
               case EVENT_ARG_QUERY:
                   Cursor cursor;
                   try {
                       cursor = resolver.query(args.uri, args.projection,
                               args.selection, args.selectionArgs,
                               args.orderBy);
                       // Calling getCount() causes the cursor window to be filled,
                       // which will make the first access on the main thread a lotfaster.
                       if (cursor != null) {
                           cursor.getCount();
                       }
                   } catch (Exception e) {
                       Log.w(TAG, e.toString());
                       cursor = null;
                   }

                   args.result = cursor;
                   break;

               case EVENT_ARG_INSERT:
                   args.result = resolver.insert(args.uri, args.values);
                   break;

               case EVENT_ARG_UPDATE:
                   args.result = resolver.update(args.uri, args.values,args.selection,
                           args.selectionArgs);
                   break;

               case EVENT_ARG_DELETE:
                   args.result = resolver.delete(args.uri, args.selection,args.selectionArgs);
                   break;
           }

           // passing the original token value back to the caller
           // on top of the event values in arg1.
           Message reply = args.handler.obtainMessage(token);
           reply.obj = args;
           reply.arg1 = msg.arg1;

           if (localLOGV) {
               Log.d(TAG, "WorkerHandler.handleMsg: msg.arg1=" + msg.arg1
                       + ", reply.what=" + reply.what);
           }

           reply.sendToTarget();
       }
    }

    总结:
   Handler有两个作用,Handler用在一个线程中,就是实现异步操作。用在不同的线程之间,那就是异步操作加线程间通信。 

AsyncQueryHandler封装了调用者线程与工作线程的交互过程。交互的主体是两个Handler,一个运行在调用者线程中,一个运行在工作者线程中。通过提供onXXXComplete的回调接口,实现事件的完成处理。

流程:startQuery->WorkerHandler::handleMessage->AsyncQueryHandler::handleMessage->onDeleteComplete

这个类展示了如何利用Handler实现线程间异步事件的交互,简单实用、值得参考!


每天进步一点点之AsyncQueryHandler学习

AsyncQueryHandler:异步的查询操作帮助类,其实它同样可以处理增删改

1。AsyncQueryHandler的作用

查询其API便可知,它担供:

startInsert

startDelete

startUpdate

startQuery

这四个操作,并提供相对应的onXXXComplete方法,以供操作完数据库后进行其它的操作,这四个onXXXComplete方法都是空实现,以便我们只需要去实现我们关注的操作。

 

2。为什么要使用AsyncQueryHandler

当然你也可以使用ContentProvider去操作数据库。这在数据量很小的时候是没有问题的,但是如果数据量大了,可能导致UI线程发生ANR事件。当然你也可以写个Handler去做这些操作,只是你每次使用ContentProvider时都要再写个Handler,必然降低了效率。

因此API提供了一个操作数据库的通用方法。

3。如何使用AsyncQueryHandler

你只需要继承AsyncQueryHandler类,并提供onXXXComplete方法的实现(可以实现任何一个或多个,当然你也可以一个也不实现,如果你不关注操作数据库的結果),在你的实现中做一些对数据库操作完成的处理。

使用时直接调用startXXX方法即可。传入的通用参数如下:

inttoken,一个令牌,需要跟onXXXComplete方法传入的一致。(当然你也可以不一致,同样在数据库的操作结束后会调用对应的onXXXComplete方法)

Objectcookie,你想传给onXXXComplete方法使用的一个对象。(没有的话传递null即可。基本发现这个变量没太大作用)

Uri uri,操作数据的URi

 

4。AsyncQueryHandler还为我们做了什么

AsyncQueryHandler中使用了一个WeakReference<ContentResolver>对象,即ContentResolver的弱引用  作用:当contentProvied发生变化时候同步更新仍可以通过使用AsyncQueryHandler类来达到这一要求(暂时还没理解这个作用)

同时,在它执行操作数据库时,吃掉了所有的异常。见如下代码。

 catch (Exception e) {
                       Log.w(TAG, e.toString());
                       cursor = null;
                   }