非主线程更新UI:AsyncTask

练习心得

  • AsyncTask必须要在主线程中执行execute,而且仅能被执行一次,否则报异常
  • AsyncTask里的所有方法都是系统回调的,不要人为调用
  • 耗时任务在doInBackground方法里执行,该方法会启动新线程执行,注意不要在其中更新UI,会报异常
  • AsyncTask除doInBackground方法外的其它方法均是在主线程中执行的

代码样例

/**
 * Created by Rambo
 */

public class MyActivity extends MainActivity {

    private EditText myEditText = null;
    private Button stop = null;
    private HashMap asynTaskParams = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        myEditText = (EditText) findViewById(R.id.myEditText);
        stop = (Button) findViewById(R.id.stop);

        /**
         * 应用AsyncTask实线非主线程更新UI的操作
         */
        final AsyncTask, Integer, String> asyncTask = new AsyncTask, Integer, String>() {
            /**
             * 启动新的线程执行doInBackground代码,不能在其中更新UI,可通过调用publishProgress触发
             * onProgressUpdate方法并在其中更新UI
             * @param hashMaps
             * @return
             */
            @Override
            protected String doInBackground(HashMap... hashMaps) {
                Log.v(TAG, "doInBackground thread:" + Thread.currentThread().getName());
                try {
                    for (int index = 0; index < 5; index++) {
                        Thread.sleep(1000);
                        publishProgress(new Integer(index));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 该返回值会传递给onPostExecute、onCancelled(String s)
                return "finish";
            }

            /**
             *
             */
            @Override
            protected void onCancelled() {
                super.onCancelled();
                Log.v(TAG, "onCancelled() onCancelled!");
                Log.v(TAG, "onCancelled() thread:" + Thread.currentThread().getName());
            }

            @Override
            protected void onCancelled(String s) {
                super.onCancelled(s);
                Log.v(TAG, "onCancelled(String s) onCancelled!");
                Log.v(TAG, "onCancelled " + s);
                Log.v(TAG, "onCancelled(String s) thread:" + Thread.currentThread().getName());
            }

            /**
             * doInBackground执行后触发该方法
             * @param s doInBackground方法的返回值
             */
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Log.v(TAG, "onPostExecute onPostExecute!");
                Log.v(TAG, "onPostExecute " + s);
                Log.v(TAG, "onPostExecute thread:" + Thread.currentThread().getName());
            }

            /**
             * doInBackground执行前触发该方法
             */
            @Override
            protected void onPreExecute() {
                Log.v(TAG, "onPreExecute running!");
                Log.v(TAG, "onPreExecute thread:" + Thread.currentThread().getName());
                asynTaskParams.put("param1", "param1");
                asynTaskParams.put("param2", "param2");
                asynTaskParams.put("param3", "param3");
                myEditText.setText("马上开始");


            }

            /**
             * 由publishProgress触发调用,改方法在doInBackground中调用
             * @param values 调用publishProgress时传递的动态参数(动态参数只能有一个而且必须是最后一个参数)
             */
            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                Log.v(TAG, "onProgressUpdate running!");
                Log.v(TAG, "onProgressUpdate thread:" + Thread.currentThread().getName());
                myEditText.setText(Integer.toString(values[0].intValue()));
            }
        };
        //每个AsyncTask仅能被执行1次,而且仅能在主线程执行
        asyncTask.execute(asynTaskParams);

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                asyncTask.cancel(true);
            }
        });

    }

}

运行日志如下:

非主线程更新UI:AsyncTask_第1张图片
运行日志

执行过程中调用asyncTask.cancel(true),运行日志如下:

非主线程更新UI:AsyncTask_第2张图片
取消执行

你可能感兴趣的:(非主线程更新UI:AsyncTask)