`
huangqinqin
  • 浏览: 365647 次
  • 性别: Icon_minigender_2
  • 来自: 福州
社区版块
存档分类
最新评论

Handler Looper的结合

 
阅读更多
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.content;

import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import java.lang.ref.WeakReference;

/**
* A helper class to help make handling asynchronous {@link ContentResolver}
* queries easier.
*/
public abstract class AsyncQueryHandler extends Handler {
    private static final String TAG = "AsyncQuery";
    private static final boolean localLOGV = false;

    private static final int EVENT_ARG_QUERY = 1;
    private static final int EVENT_ARG_INSERT = 2;
    private static final int EVENT_ARG_UPDATE = 3;
    private static final int EVENT_ARG_DELETE = 4;

    /* package */ final WeakReference<ContentResolver> mResolver;

    private static Looper sLooper = null;

    private Handler mWorkerThreadHandler;

    protected static final class WorkerArgs {
        public Uri uri;
        public Handler handler;
        public String[] projection;
        public String selection;
        public String[] selectionArgs;
        public String orderBy;
        public Object result;
        public Object cookie;
        public ContentValues values;
    }

    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 lot faster.
                        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();
        }
    }

    public AsyncQueryHandler(ContentResolver cr) {
        super();
        mResolver = new WeakReference<ContentResolver>(cr);
        synchronized (AsyncQueryHandler.class) {
            if (sLooper == null) {
                HandlerThread thread = new HandlerThread("AsyncQueryWorker");
                thread.start();

                sLooper = thread.getLooper();
            }
        }
        mWorkerThreadHandler = createHandler(sLooper);
    }

    protected Handler createHandler(Looper looper) {
        return new WorkerHandler(looper);
    }

    /**
     * This method begins an asynchronous query. When the query is done
     * {@link #onQueryComplete} is called.
     *
     * @param token A token passed into {@link #onQueryComplete} to identify
     *  the query.
     * @param cookie An object that gets passed into {@link #onQueryComplete}
     * @param uri The URI, using the content:// scheme, for the content to
     *         retrieve.
     * @param projection A list of which columns to return. Passing null will
     *         return all columns, which is discouraged to prevent reading data
     *         from storage that isn't going to be used.
     * @param selection A filter declaring which rows to return, formatted as an
     *         SQL WHERE clause (excluding the WHERE itself). Passing null will
     *         return all rows for the given URI.
     * @param selectionArgs You may include ?s in selection, which will be
     *         replaced by the values from selectionArgs, in the order that they
     *         appear in the selection. The values will be bound as Strings.
     * @param orderBy How to order the rows, formatted as an SQL ORDER BY
     *         clause (excluding the ORDER BY itself). Passing null will use the
     *         default sort order, which may be unordered.
     */
    public void startQuery(int token, Object cookie, Uri uri,
            String[] projection, String selection, String[] selectionArgs,
            String orderBy) {
        // Use the token as what so cancelOperations works properly
        Message msg = mWorkerThreadHandler.obtainMessage(token);
        msg.arg1 = EVENT_ARG_QUERY;

        WorkerArgs args = new WorkerArgs();
        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);
    }

    /**
     * Attempts to cancel operation that has not already started. Note that
     * there is no guarantee that the operation will be canceled. They still may
     * result in a call to on[Query/Insert/Update/Delete]Complete after this
     * call has completed.
     *
     * @param token The token representing the operation to be canceled.
     *  If multiple operations have the same token they will all be canceled.
     */
    public final void cancelOperation(int token) {
        mWorkerThreadHandler.removeMessages(token);
    }

    /**
     * This method begins an asynchronous insert. When the insert operation is
     * done {@link #onInsertComplete} is called.
     *
     * @param token A token passed into {@link #onInsertComplete} to identify
     *  the insert operation.
     * @param cookie An object that gets passed into {@link #onInsertComplete}
     * @param uri the Uri passed to the insert operation.
     * @param initialValues the ContentValues parameter passed to the insert operation.
     */
    public final void startInsert(int token, Object cookie, Uri uri,
            ContentValues initialValues) {
        // Use the token as what so cancelOperations works properly
        Message msg = mWorkerThreadHandler.obtainMessage(token);
        msg.arg1 = EVENT_ARG_INSERT;

        WorkerArgs args = new WorkerArgs();
        args.handler = this;
        args.uri = uri;
        args.cookie = cookie;
        args.values = initialValues;
        msg.obj = args;

        mWorkerThreadHandler.sendMessage(msg);
    }

    /**
     * This method begins an asynchronous update. When the update operation is
     * done {@link #onUpdateComplete} is called.
     *
     * @param token A token passed into {@link #onUpdateComplete} to identify
     *  the update operation.
     * @param cookie An object that gets passed into {@link #onUpdateComplete}
     * @param uri the Uri passed to the update operation.
     * @param values the ContentValues parameter passed to the update operation.
     */
    public final void startUpdate(int token, Object cookie, Uri uri,
            ContentValues values, String selection, String[] selectionArgs) {
        // Use the token as what so cancelOperations works properly
        Message msg = mWorkerThreadHandler.obtainMessage(token);
        msg.arg1 = EVENT_ARG_UPDATE;

        WorkerArgs args = new WorkerArgs();
        args.handler = this;
        args.uri = uri;
        args.cookie = cookie;
        args.values = values;
        args.selection = selection;
        args.selectionArgs = selectionArgs;
        msg.obj = args;

        mWorkerThreadHandler.sendMessage(msg);
    }

    /**
     * This method begins an asynchronous delete. When the delete operation is
     * done {@link #onDeleteComplete} is called.
     *
     * @param token A token passed into {@link #onDeleteComplete} to identify
     *  the delete operation.
     * @param cookie An object that gets passed into {@link #onDeleteComplete}
     * @param uri the Uri passed to the delete operation.
     * @param selection the where clause.
     */
    public final void startDelete(int token, Object cookie, Uri uri,
            String selection, String[] selectionArgs) {
        // Use the token as what so cancelOperations works properly
        Message msg = mWorkerThreadHandler.obtainMessage(token);
        msg.arg1 = EVENT_ARG_DELETE;

        WorkerArgs args = new WorkerArgs();
        args.handler = this;
        args.uri = uri;
        args.cookie = cookie;
        args.selection = selection;
        args.selectionArgs = selectionArgs;
        msg.obj = args;

        mWorkerThreadHandler.sendMessage(msg);
    }

    /**
     * Called when an asynchronous query is completed.
     *
     * @param token the token to identify the query, passed in from
     *            {@link #startQuery}.
     * @param cookie the cookie object passed in from {@link #startQuery}.
     * @param cursor The cursor holding the results from the query.
     */
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
        // Empty
    }

    /**
     * Called when an asynchronous insert is completed.
     *
     * @param token the token to identify the query, passed in from
     *        {@link #startInsert}.
     * @param cookie the cookie object that's passed in from
     *        {@link #startInsert}.
     * @param uri the uri returned from the insert operation.
     */
    protected void onInsertComplete(int token, Object cookie, Uri uri) {
        // Empty
    }

    /**
     * Called when an asynchronous update is completed.
     *
     * @param token the token to identify the query, passed in from
     *        {@link #startUpdate}.
     * @param cookie the cookie object that's passed in from
     *        {@link #startUpdate}.
     * @param result the result returned from the update operation
     */
    protected void onUpdateComplete(int token, Object cookie, int result) {
        // Empty
    }

    /**
     * Called when an asynchronous delete is completed.
     *
     * @param token the token to identify the query, passed in from
     *        {@link #startDelete}.
     * @param cookie the cookie object that's passed in from
     *        {@link #startDelete}.
     * @param result the result returned from the delete operation
     */
    protected void onDeleteComplete(int token, Object cookie, int result) {
        // Empty
    }

    @Override
    public void handleMessage(Message msg) {
        WorkerArgs args = (WorkerArgs) msg.obj;

        if (localLOGV) {
            Log.d(TAG, "AsyncQueryHandler.handleMessage: msg.what=" + msg.what
                    + ", msg.arg1=" + msg.arg1);
        }

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

        // pass token back to caller on each callback.
        switch (event) {
            case EVENT_ARG_QUERY:
                onQueryComplete(token, args.cookie, (Cursor) args.result);
                break;

            case EVENT_ARG_INSERT:
                onInsertComplete(token, args.cookie, (Uri) args.result);
                break;

            case EVENT_ARG_UPDATE:
                onUpdateComplete(token, args.cookie, (Integer) args.result);
                break;

            case EVENT_ARG_DELETE:
                onDeleteComplete(token, args.cookie, (Integer) args.result);
                break;
        }
    }
}
分享到:
评论

相关推荐

    应用源码之HandlerLooper2.zip

    本资料"应用源码之HandlerLooper2.zip"显然是针对这一主题进行深入解析的源码学习资源。以下是关于`Handler`、`Looper`和`MessageQueue`的详细知识讲解。 1. **Handler(处理器)**: `Handler`是Android中的一个...

    Android Handler Looper

    Handler、Looper和Message三者结合使用,构建了一个消息处理系统,使得非UI线程可以安全地向UI线程发送消息并执行相关操作。 首先,我们来理解Handler。Handler是Android中的一个类,它的主要职责是在特定的线程...

    Handler,Looper和Message

    3. 定时任务:结合`AlarmManager`或`CountDownTimer`,`Handler`可以实现定时执行任务的功能。 在实际开发中,要注意以下几点: - 为了避免内存泄漏,确保在不再使用`Handler`时调用`removeCallbacksAndMessages...

    Android线程模式(handler,thread,looper)

    本文将深入探讨Android中的三种主要线程模式:Handler、Thread以及Looper,并结合源码分析它们的工作原理。 首先,我们来理解一下Android应用的基本运行环境。Android系统默认在主线程(UI线程)中执行所有的用户...

    Looper、Handler与HandlerThread

    在Android开发中,Looper、Handler和HandlerThread是三个非常重要的组件,它们构成了Android消息处理机制的基础,用于在主线程中处理来自其他线程的消息。理解并熟练运用这三个组件,对于编写高效、响应迅速的...

    Android单线程模型中Message、Handler、Message Queue、Looper之间的关系---附实例源码

    消息的类型,在Handler类中的handleMessage方法中得到单个的消息进行处理,在单线程模型下,为了线程通信问题,Android设计了一个Message Queue(消息队列), 线程间可以通过该Message Queue并结合Handler和Looper组件...

    handler结合Thread异步加载网络图片

    3. **创建Handler**:在主线程中,创建一个`Handler`实例,通常会在构造函数中传入`Looper.getMainLooper()`,表示消息将被主线程的`Looper`处理。 ```java Handler mainHandler = new Handler(Looper....

    android Looper demo

    现在,让我们结合`android Looper demo`的场景来看这些概念的应用: 1. 主线程内消息发送:在主线程中创建一个`Handler`,然后通过这个`Handler`发送消息。这通常用于更新UI或者执行其他与UI相关的操作。由于主线程...

    Handler结合thread 异步加载网络图片

    在"Handler结合thread异步加载网络图片"的场景中,我们可以这样操作: 1. **创建后台线程**:创建一个Thread实例,重写run()方法,用于执行网络请求。在这个方法里,我们使用HttpURLConnection或OkHttp等库下载图片...

    关于Looper的使用

    - **定时任务**:结合Handler和Runnable,可以实现定时执行某些任务。 - **广播接收器**:在自定义的IntentService中,可以通过Looper处理接收到的广播,实现服务的异步响应。 - **事件总线**:如使用EventBus库,...

    android Looper

    结合博客文章中的“源码”标签,我们可以推测作者可能深入解析了`Looper`、`Handler`和`Message`的内部实现,包括它们如何协作,以及在Java或Android的源代码层面是如何组织和运行的。这可能会涉及线程同步、内存...

    android 中Handler 的几种写法

    在Android开发中,`Handler`是一个至关重要的组件,它用于在主线程中处理来自其他线程的消息,确保UI更新...在实际开发中,结合`Handler`、`Looper`、`Thread`以及`Runnable`,可以构建出复杂而稳定的多线程运行机制。

    安卓handler的使用

    在实际开发中,Handler还可以结合MessageQueue、Callback等类实现更复杂的线程间交互。例如,可以定义自定义的Message子类来携带特定类型的数据,或者使用`sendEmptyMessage(int what)`发送无数据的消息。 通过以上...

    handler回调机制 demo

    在Android开发中,Handler回调机制是用于解决线程间通信的核心技术之一,它与Looper和Message紧密相连,共同构建了Android的消息处理系统。本篇将深入解析Handler回调机制,并通过提供的"handler的回调机制"DEMO来...

    handler全面分析

    Handler、Looper和Message三者共同构建了一个消息传递机制,使得非UI线程可以安全地与UI线程交互。下面我们将深入探讨Handler的各个方面。 首先,我们来理解Handler的基本概念。Handler是一个接口,它定义了如何...

    Android Handler线程间的调度

    本文将深入探讨“Android Handler线程间的调度”这一主题,并结合提供的资源——MessageProject,来阐述相关知识点。 首先,Handler是Android中的一个关键类,它允许开发者在不同的线程之间发送和处理消息。Handler...

    handler简单使用

    此外,Handler还常与AsyncTask、BroadcastReceiver等组件结合使用,实现异步操作的回调或者跨组件通信。例如,在一个后台服务中,可以通过Handler将数据传递到UI线程,更新界面状态。 总之,Handler是Android中进行...

    Handler机制

    为了解决这个问题,Android引入了Handler、Looper和Message三者结合的工作模式。 1. **Handler**:Handler对象是消息处理的核心,它负责发送和处理消息。当我们创建一个Handler实例时,通常会在构造函数中传入一个...

    Handler消息机制和异步加载

    Handler消息机制是Android系统中处理线程间通信的重要方式,它与Looper和Message紧密关联,构建了一套完整的异步处理模型。在这个模型中,Handler主要负责发送和处理Message,Looper负责在一个线程中循环地读取...

    Handler与AsyncTask使用示例

    现在让我们来看一个`Handler`和`AsyncTask`结合使用的示例: ```java public class MainActivity extends AppCompatActivity { private Handler mHandler = new Handler() { @Override public void ...

Global site tag (gtag.js) - Google Analytics