zoukankan      html  css  js  c++  java
  • android-多线程

    AsyncTask

    public class MyAsyncTask extends AsyncTask<String, Integer, Integer> {
        @Override
        protected void onProgressUpdate(Integer... values) {
            // 更新进度条通知或其它UI元素
            super.onProgressUpdate(values);
        }
        
        @Override
        protected void onPostExecute(Integer result) {
            // 通过UI更新、对话框或通知报告结果
            super.onPostExecute(result);
        }
        
        @Override
        protected Integer doInBackground(String... params) {
            int myProgress = 0;
            // 执行后台处理任务,更新myProgress
            for (int i = 0; i < 10; i++) {
                myProgress++;
                publishProgress(myProgress);
            }
            
            // 返回将传递给onPostExecute
            return myProgress;
        }
    }
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            //执行一个异步任务
            new MyAsyncTask().execute("inputString1","inputString2");
        }
    }

    Thread

    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
                
            //执行一个异步任务
            Thread thread = new Thread(null, doBackgroundThreadProcessing, "Background");
            thread.start();
        }
        
        private Runnable doBackgroundThreadProcessing = new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                backgroundThreadProcessing();
            }
        };
        
        private Handler handler = new Handler();
        private void backgroundThreadProcessing() {
            // TODO 在这完成耗时的工作
            
            //将更新从后台发送到用户界面上,方法1:post方法完成返回到主线程
            handler.post(doUpdateGUI);
            
            //方法2:runOnUiThread
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    updateGUI();
                }
            });
        }
        
        private Runnable doUpdateGUI = new Runnable() {
            public void run() {
                updateGUI();
            };
        };
        
        private void updateGUI() {
            Context context = getApplicationContext();
            String msg = "To health and happiness!";
            int duration = Toast.LENGTH_SHORT;
            
            Toast toast = Toast.makeText(context, msg, duration);
            toast.show();
        }
    }
  • 相关阅读:
    站点设计
    站点规划
    自动登录百度网盘
    windows下配置Python虚拟环境
    拖拽效果
    jQuery 基础
    js操作BOM
    js操作DOM
    selenium+js自动连接Magic_WiFi
    弹框
  • 原文地址:https://www.cnblogs.com/mingfung-liu/p/4561498.html
Copyright © 2011-2022 走看看