AsyncTask的使用
来源:互联网 发布:javascript hide div 编辑:程序博客网 时间:2024/06/18 13:22
AsyncTask可以实现很简单的实现异步操作。
AsyncTask < Params, Progress, Progress >继承AsyncTask的时候需要定义三个参数的类型,第一个参数是调用异步方法时传进去的参数,如asyncTask.execute(1000);这里第一个参数使用的是Integer。第二个参数是调用publishProgress时传递的参数,改方法可以用于更新异步线程执行的进度,调用publishProgress会触发onProgressUpdate方法。第三个参数是异步方法执行结束后返回的结果。
AsyncTask可以重写5个方法:onPreExecute、onCancelled(onCancelled有两个方法,一个是不带参数的一个是带参数的,参数为执行结果)、doInBackground、onProgressUpdate、onPostExecute。
主线程使用asyncTask.execute来启动AsyncTask,启动后会先调用onPreExecute,可以执行一些初始化等的操作,然后会进行真正的异步方法doInBackground,在doInBackground中可以使用publishProgress触发onProgressUpdate方法。onPostExecute为异步方法结束后,处理结果的方法。使用asyncTask.cancel(true)可以中断异步方法,asyncTask.cancel(false)会运行异步方法运行结束。
以下是我自己写的测试代码:
package com.study.test;import java.util.HashMap;import java.util.Map;import android.annotation.SuppressLint;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;public class MainActivity extends Activity { private Button button; private ProgressBar progressBar; private TextView textView; private Button cancelled; MAsyncTask asyncTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); cancelled = (Button) findViewById(R.id.button2); progressBar = (ProgressBar) findViewById(R.id.progressBar1); textView = (TextView) findViewById(R.id.textView1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setVisibility(View.GONE); asyncTask = new MAsyncTask(); asyncTask.execute(1000); } }); cancelled.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub asyncTask.cancel(true); } }); } public class MAsyncTask extends AsyncTask<Integer, Integer, Map<String, Integer>> { long starttime; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); starttime = System.currentTimeMillis(); } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); } @SuppressLint("NewApi") @Override protected void onCancelled(Map<String, Integer> result) { // TODO Auto-generated method stub super.onCancelled(result); } @Override protected Map<String, Integer> doInBackground(Integer... params) { // TODO Auto-generated method stub long span = 0; while (span < params[0]) { span = System.currentTimeMillis() - starttime; publishProgress(Integer .valueOf((int) (span * 1f / params[0] * 100))); } return new HashMap<String, Integer>(); } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); progressBar.setProgress(values[0]); } @Override protected void onPostExecute(Map<String, Integer> result) { // TODO Auto-generated method stub super.onPostExecute(result); if (result != null) { textView.setVisibility(View.VISIBLE); } } }}
2 0
- AsyncTask的使用
- AsyncTask类的使用
- Android 的AsyncTask使用
- AsyncTask的基本使用
- AsyncTask的简单使用
- AsyncTask的简单使用
- Android AsyncTask的使用
- AsyncTask的使用
- AsyncTask的简单使用
- Android AsyncTask 的使用
- AsyncTask的使用
- AsyncTask类的使用
- AsyncTask的使用
- AsyncTask的使用步骤
- AsyncTask的使用
- 使用Android的AsyncTask
- Android AsyncTask 的使用
- AsyncTask的使用
- jaylptpeu
- TableView的详细使用
- leetcode - Reverse Words in a String
- WPF 自定义属性和触发器
- 硬件电路之电感,磁珠,零欧姆电阻的应用
- AsyncTask的使用
- Guide to understanding XSS – XSS payloads, attack vectors, BeEF hooking, MiTM with Shank and some hi
- LeetCode89:Gray Code
- 自定义“菊花dialog”
- cas-server异常不会后台显示而是变成json显示在页面。
- MySQL查看和修改表的存储引擎
- AIX Study之--AIX用户资源访问限制配置
- SQL Server 跨服务器 不同数据库之间复制表的数据的方法!
- GRE写作必备句型