zoukankan      html  css  js  c++  java
  • android下的异步任务

    异步任务一般用在加载一些网络资源的时候用,主要的实现方法是新建一个类来继承AsyncTask这个父类,然后复写该类下面的一些方法,其中doInBackground方法是必须要的,下面看代码

    package com.example.test;
    
    import android.os.AsyncTask;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class ProgressBarAsyncTask extends AsyncTask<Integer, Integer, String> {
        private TextView textView;
        private ProgressBar progressBar;
    
        public ProgressBarAsyncTask(TextView textView, ProgressBar progressBar) {
            super();
            this.textView = textView;
            this.progressBar = progressBar;
        }
    
        @Override
        protected void onPreExecute() {// in UI thread
            // TODO Auto-generated method stub
            super.onPreExecute();
            textView.setText("onPreExecute开始运行");
        }
    
        @Override
        protected String doInBackground(Integer... params) {// MainActivity传进来的参数
            // TODO Auto-generated method stub
            NetOperatior netOperatior = new NetOperatior();
            System.out.println("params" + params[0].intValue());// 测试传进来的值
            int i;
            for (i = 10; i <= 100; i += 10) {
                netOperatior.operate();// current thread sleep 1s
                publishProgress(i);// 执行onProgressUpdate(Integer... values)方法
            }
            return i + params[0].intValue() + "";
        }
    
        @Override
        protected void onPostExecute(String result) {// 由doInBackground(Integer...
                                                        // params)方法返回的值进行传递,在主UI线程中运行
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            textView.setText("the operation have done,the current value of i is ="
                    + result);
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            int value = values[0];
            progressBar.setProgress(value);
            super.onProgressUpdate(values);
        }
    
    }

    注意在这个类中的构造方法需要传递两个参数,一个是TextView,一个是ProgressBar,而在AsyncTask<Integer, Integer, String>中是你需要用到的类型的变量,其中第一个变量类型是由MainActivity.java中执行execute方法传递进去的参数。

    那么MainActivity.java的代码:

    package com.example.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        private TextView textView;
        private ProgressBar progressBar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = (TextView) findViewById(R.id.textView1);
            progressBar = (ProgressBar) findViewById(R.id.progressBar1);
            ProgressBarAsyncTask progressBarAsyncTask = new ProgressBarAsyncTask(
                    textView, progressBar);
            progressBarAsyncTask.execute(1000);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    View Code

    在异步任务类中有一个NetOperatior类,该类的用于主要用于模拟下载任务,里面的方法就是让当前的线程休息一秒,注意这里的线程不是在UI线程中执行的,而OnPreExecute()方法和OnPostExecute()方法是在UI线程中执行的,所以在这里都可以与界面进行交互。

    NetOperatior.java:

    package com.example.test;
    
    public class NetOperatior {
        public void operate() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Windows phone 墓碑机制的一些源码
    关于Image一些比较抽象的东西(Build Type与 同步以及异步加载的差异)
    自定义控件之Button控件的自定义
    Java集合最全解析,学集合,看这篇就够用了!!!
    看完让你彻底搞懂Websocket原理
    别人的前途我不敢决定
    花一年的时间让自己过得像个人样
    开篇
    你看得懂吗?
    反思
  • 原文地址:https://www.cnblogs.com/sowhat4999/p/4496713.html
Copyright © 2011-2022 走看看