在 Android中的AsyncTask异步任务的简介 一文中。已经对 安卓 异步任务操作做了简单的介绍。这里,直接将上文中的异步任务做了一个实例。实现异步操作更新UI线程,相比开启子线程更新来说逻辑性更加合理

下面内容。可直接拷贝到项目中继而运行測试

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"
tools:context=".MainActivity" > <ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="58dp" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/progressBar"
android:layout_marginTop="18dp"
android:text="TextView" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Button" /> </RelativeLayout>

MainActivity.java

package com.example.asynctaskdemo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView tv;
private ProgressBar progressBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
TestAsyncTask testAsyncTask = new TestAsyncTask();
testAsyncTask.execute(100);
}
});
tv = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} /*
* @TestAsyncTaskInteger 參数Integer 进度String 返回值 类型
*/
class TestAsyncTask extends AsyncTask<Integer, Integer, String> { @Override
protected void onPreExecute() {
// 第一个运行方法
super.onPreExecute();
} @Override
protected String doInBackground(Integer... params) {
// 第二个运行方法,onPreExecute()运行完后运行
for (int i = 0; i <= 100; i++) {
progressBar.setProgress(i);
publishProgress(i);
try {
Thread.sleep(params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "运行完成";
} @Override
protected void onProgressUpdate(Integer... progress) {
// 这个函数在doInBackground调用publishProgress时触发。尽管调用时仅仅有一个參数
// 可是这里取到的是一个数组,所以要用progesss[0]来取值
// 第n个參数就用progress[n]来取值
tv.setText(progress[0] + "%");
super.onProgressUpdate(progress);
} @Override
protected void onPostExecute(String result) {
// doInBackground返回时触发。换句话说。就是doInBackground运行完后触发
// 这里的result就是上面doInBackground运行后的返回值,所以这里是"运行完成"
setTitle(result);
super.onPostExecute(result);
} }
}

对于各參数及各方法的介绍使用。均已在凝视中解释,关于很多其它 安卓 方面的开法 可加Q群 104725817 讨论

最新文章

  1. iScroll-js—“smooth scrolling for the web”
  2. Lintcode: Maximum Subarray Difference
  3. mysql通过binlog日志来恢复数据
  4. 黄聪:百度知道中对HTML字符实体、字符编号,&amp;开头字符的使用
  5. 研究CPU的好文章以及博客
  6. Python自动单元测试框架
  7. 基本API-StdIn.java
  8. SQL索引优化
  9. wamp集成环境php多版本搭建(php5.5,php5.6,php7.0.6)
  10. Start an installation from GRUB
  11. MongoDB资料--Java驱动, Hadoop驱动, Spark使用
  12. A5营销访谈:卢松松和你聊新媒体运营那些事
  13. C#,json字符串转换成Json对象
  14. C++之继承和动态内存分配
  15. 使用php完成常见的&quot;文件上传&quot;功能
  16. JS中函数参数和函数返回值的理解
  17. java程序设计----学生基本信息管理系统
  18. OVS中arp响应的流表的实现
  19. NetBeans部署项目(Extjs)报错(二)
  20. js实现浏览器调用电脑的摄像头拍照

热门文章

  1. C/C++ 浮点数比较问题
  2. 洛谷 P1894 [USACO4.2]完美的牛栏The Perfect Stall
  3. 使用Love2D引擎开发贪吃蛇游戏
  4. IOS版本号被拒的经历
  5. log4j.propertie配置具体解释
  6. DICOM医学图像处理:fo-dicom网络传输之 C-Echo and C-Store
  7. 三种SVM的对偶问题
  8. (转)C/C++ 宏详解
  9. Java中异常处理之try和catch代码块的使用
  10. mysql实战45讲 (三) 事务隔离:为什么你改了我还看不见 极客时间读书笔记