Android AsyncTask 异步任务操作
1:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <ProgressBar
android:layout_below="@id/tvInfo"
android:id="@+id/asyncPb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/> </RelativeLayout>
2:MainActivity.java
package com.example.async; import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity; public class MainActivity extends Activity {
private ProgressBar asyncPb = null;
private TextView tvInfo = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tvInfo = (TextView)findViewById(R.id.tvInfo); String params = "Welcome to here";
new MyAsyncTask().execute(params);
} private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected void onPreExecute() {
//做一些预处理
asyncPb = (ProgressBar)findViewById(R.id.asyncPb);
asyncPb.setVisibility(View.VISIBLE);
} @Override
protected String doInBackground(String... params) {
//执行耗时操作,网络任务、文件操作、数据库操作、复杂计算操作
int myProgress = 0;
int length = params[0].length(); for(int i=1; i<=length; i++){
myProgress = i;
//模拟耗时操作
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress((int)((myProgress/(float)length)*100));
} //它将传递给onPostExecute
return params[0];
} @Override
protected void onProgressUpdate(Integer... values) {
//更新进度条
asyncPb.setProgress(values[0]);
tvInfo.setText("已加载:"+(values[0])+"%");
} @Override
protected void onPostExecute(String result) {
//更新UI
tvInfo.setText("加载完成:"+result);
}
}
}
Android AsyncTask 异步任务操作的更多相关文章
- Android --AsyncTask异步任务(一)
1.为什么要异步任务 Android单线程模式 耗时操作放在非主线程(UI线程)中执行 我们都知道Android是单线程模式,只有主线程才能对UI操作,简称UI线程.当然这样的好处是:保证UI的稳定性 ...
- Android AsyncTask异步加载WebAPI
之前做的程序一直存在很多问题,因为需要加载的Activity需要从网络加载数据.并没有完全正确的使用异步的方法去加载! 之前用的虽然是AsyncTask,但是在加载完成的时候还是并没有使用AsyncT ...
- android AsyncTask异步下载并更新进度条
AsyncTask异步下载并更新进度条 //如果不是很明白请看上篇文章的异步下载 AsyncTask<String, Integer, String> 第一个参数:String 传入 ...
- Android AsyncTask异步任务(二)
之前我们讲过了AsyncTask 的生命周期(onPreExecute-->doInBackground-->onProgressUpdate-->onPostExecute),今天 ...
- android AsyncTask异步任务(笔记)
AsyncTask是一个专门用来处理后台进程与UI线程的工具.通过AsyncTask,我们可以非常方便的进行后台线程和UI线程之间的交流. 那么AsyncTask是如何工作的哪. AsyncTask拥 ...
- android内部培训视频_第四节(1)_异步网络操作
第四节(1):异步网络操作 一.结合asyncTask下载网络图片 1.定义下载类,继承自asyncTask,参数分别为:String(url地址),Integer(刻度,本例没有用到),BitMa ...
- Android线程管理之AsyncTask异步任务
前言: 前面几篇文章主要学习了线程以及线程池的创建与使用,今天来学习一下AsyncTask异步任务,学习下AsyncTask到底解决了什么问题?然而它有什么弊端?正所谓知己知彼百战百胜嘛! 线程管理相 ...
- Android 多线程----AsyncTask异步任务详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- Android中AsyncTask异步
今天我们学习了 AsyncTack, 这是一个异步任务. 那么这个异步任务可以干什么呢? 因为只有UI线程,即主线程可以对控件进行更新操作.好处是保证UI稳定性,避免多线程对UI同时操作. 同时要把耗 ...
随机推荐
- 为什么Application_BeginRequest会执行两次
大家也看到了,很奇怪的是我们明明就请求了一个页面,页面中也没有其他的图片请求.为什么Application_BeginRequest会被执行了两次呢?!既然他请求,那我们看看他到底在请求什么就是 ...
- mv,Directory not empty不能目录覆盖
一.mv /test1/* /test2/test1rm -rf /test1 二. You can however use rsync with the --remove-source-files ...
- 转:ASP.Net MVC:校验、AJAX与过滤器
原文地址:http://blog.jobbole.com/85005/ 一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.Componen ...
- CSS flex让所有灵活的项目都带有相同的长度,忽略它们的内容:
.flexbox { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; displa ...
- Swift字符串常用操作总结
转自:http://www.jianshu.com/p/52e7580166ff 1.string转换为Int/Long/Float/Double/Bool等 var str1="100&q ...
- 照猫画虎学gnuplot之简单介绍
简单介绍:Gnuplot是一个命令行驱动的科学画图工具,可将数学函数或数值资料以平面图或立体图的形式画在不同种类终端机或画图输出装置上. 它是由Colin Kelley 和 Thomas Willia ...
- OC教程10-NSNumber具体
NSNumber简单介绍 NSNumber是数字的对象形式,由于在OC的数组和字典中仅仅同意存放对象,所以我们有时候须要转化 我们普通的类型是 123 那么 NSNumber类型的是 @123, ...
- Oracle用户解锁的三种办法及默认的用户与密码
ORA-28000: the account is locked-的解决办法 2009-11-11 18:51 ORA-28000: the account is locked 第1步:使用PL/SQ ...
- Solr 安装与集成IK中文分词器
创建wangchuanfu core 1. 在example目录下创建wangchuanfu-solr文件夹: 2. 将./solr下的solr.xml拷贝到wangchuanfu-solr目录下 ...
- 使用Xcode和Instruments调试解决iOS内存泄露【转】
转载自:http://blog.csdn.net/totogo2010/article/details/8233565 虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露 ...