Handler使用实例

Handler

1. 直接使用Handler,不带消息循环

package com.android.mms.ui;

import com.android.mms.R;
import com.android.mms.layout.LayoutManager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;


/**
 * This AsyncDialog class is used to execute a runnable in a background thread and once that
 * finishes, execute a runnable on the UI thread. If the background runnable task takes longer
 * than half a second, a progress modal dialog is displayed.
 *
 */
public class AsyncDialog {
    private ProgressDialog mProgressDialog;
    private final Activity mActivity;
    private final Handler mHandler;

    public AsyncDialog(Activity activity) {
        mActivity = activity;
        mHandler = new Handler();
    }

    /**
     * Asynchronously executes a task while blocking the UI with a progress spinner.
     *
     * Must be invoked by the UI thread.  No exceptions!
     *
     * @param backgroundTask the work to be done in the background wrapped in a Runnable
     * @param postExecuteTask an optional runnable to run on the UI thread when the background
     * runnable is finished
     * @param dialogStringId the id of the string to be shown in the dialog
     */
    public void runAsync(final Runnable backgroundTask,
            final Runnable postExecuteTask, final int dialogStringId) {
        new ModalDialogAsyncTask(dialogStringId, postExecuteTask)
            .execute(new Runnable[] {backgroundTask});
    }

    // Shows the activity's progress spinner. Should be canceled if exiting the activity.
    private Runnable mShowProgressDialogRunnable = new Runnable() {
        @Override
        public void run() {
            if (mProgressDialog != null) {
                mProgressDialog.show();
            }
        }
    };

    public void clearPendingProgressDialog() {
        // remove any callback to display a progress spinner
        mHandler.removeCallbacks(mShowProgressDialogRunnable);
        // clear the dialog so any pending dialog.dismiss() call can be avoided
        mProgressDialog = null;
    }

    /**
     * Asynchronously performs tasks specified by Runnables.
     * Displays a progress spinner while the tasks are running.  The progress spinner
     * will only show if tasks have not finished after a certain amount of time.
     *
     * This AsyncTask must be instantiated and invoked on the UI thread.
     *
     * TODO: Need to implement a way for the background thread to pass a result to
     * the onPostExecute thread. AsyncTask already provides this functionality.
     */
    private class ModalDialogAsyncTask extends AsyncTask<Runnable, Void, Void> {
        final Runnable mPostExecuteTask;

        /**
         * Creates the Task with the specified string id to be shown in the dialog
         */
        public ModalDialogAsyncTask(int dialogStringId,
                final Runnable postExecuteTask) {
            mPostExecuteTask = postExecuteTask;
            // lazy initialization of progress dialog for loading attachments
            if (mProgressDialog == null) {
                mProgressDialog = createProgressDialog();
            }
            mProgressDialog.setMessage(mActivity.getText(dialogStringId));
        }

        /**
         * Initializes the progress dialog with its intended settings.
         */
        private ProgressDialog createProgressDialog() {
            ProgressDialog dialog = new ProgressDialog(mActivity);
            dialog.setIndeterminate(true);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            return dialog;
        }

        /**
         * Activates a progress spinner on the UI.  This assumes the UI has invoked this Task.
         */
        @Override
        protected void onPreExecute() {
            // activate spinner after half a second
            mHandler.postDelayed(mShowProgressDialogRunnable, 500);
        }

        /**
         * Perform the specified Runnable tasks on a background thread
         */
        @Override
        protected Void doInBackground(Runnable... params) {
            if (params != null) {
                try {
                    for (int i = 0; i < params.length; i++) {
                        params[i].run();
                    }

                    // Test code. Uncomment this block to test the progress dialog popping up.
//                    try {
//                        Thread.sleep(2000);
//                    } catch (Exception e) {
//                    }
                } finally {
                    // Cancel pending display of the progress bar if the background task has
                    // finished before the progress bar has popped up.
                    mHandler.removeCallbacks(mShowProgressDialogRunnable);
                }
            }
            return null;
        }

        /**
         * Deactivates the progress spinner on the UI. This assumes the UI has invoked this Task.
         */
        @Override
        protected void onPostExecute(Void result) {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
            if (mPostExecuteTask != null) {
                mPostExecuteTask.run();
            }
        }
    }
}

2. Handler+消息循环

public class TestLooperActivity extends Activity {
    
    Runnable ra;
    TextView tv;
    LooperThread lt;
    Context context;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;
        Button btn = (Button)this.findViewById(R.id.button1);
        
        lt = new LooperThread();
        
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                lt.mHandler.sendEmptyMessage(10);
            }
        });
        
        lt.start();
    }
    
    class LooperThread extends Thread {
        public Handler mHandler;

        public void run() {
            Looper.prepare();

            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    // process incoming messages here
                    if (msg.what == 10) {
                        Toast.makeText(context, "Looper worked!!", Toast.LENGTH_LONG).show();
                    }
                }
            };

            Looper.loop();
        }
    }
}



你可能感兴趣的:(Handler使用实例)