AsyncTask 异步消息处理机制
来源:互联网 发布:python 字符串倒叙截取 编辑:程序博客网 时间:2024/04/27 09:14
Android 提供了一个好用的工具---AsyncTask ,方便我们在子线程中对 UI 进行操作,AsyncTask 背后的实现原理也是基于异步消息处理机制的。下面是AsyncTask的一个小demo。
1、效果图:
2、activity_main.xml中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="进度条" android:textSize="50px" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="50pt" android:layout_gravity="center" android:max="100" android:progress="0" /> <TextView android:id="@+id/baifenbi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="30px" android:text="0" /></LinearLayout>
3、MainActivity.java中的代码:
package com.demo.asynctaskdemo;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity {private ProgressBar progress;private TextView baifenbi ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);progress = (ProgressBar) findViewById(R.id.progressBar);baifenbi = (TextView) findViewById(R.id.baifenbi) ;new MyTask().execute() ;}class MyTask extends AsyncTask<Void, Integer, Boolean> {private static final String TAG = "MyTask";/** * onPreExecute方法,在后台任务开始执行之前调用,用于进行一些界面上的初始化操作,比 如显示一个进度条对话框等。 */@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubToast.makeText(MyApplication.getContext(),"onPreExecute方法,界面上的初始化操作", 1).show();}/** * doInBackground方法,在子线程中运行,去处理所有的耗时任 务. 任务完成可以通过 return 语句来将任务的执行结果返回, * 如果 AsyncTask 的 第三个泛型参数指定的是 Void,就可以不返回任务执行结果。 @ 注意,在这个方法中是不 可以进行 UI * 操作的,如果需要更新 UI元素, 比如说反馈当前任务的执行进度,可以调 用 * publishProgress(Progress...)方法来完成。 */@Overrideprotected Boolean doInBackground(Void... params) {// TODO Auto-generated method stubLog.d(TAG, "--doInBackground" + params);int progress = 1;while (true) {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}publishProgress(progress);if (progress >= 100) {progress = 0 ;break;}progress++ ;}return true;}/** * 在后台任务中调用了 publishProgress(Progress...)方法后,这个方法就会被调用, * 方法中携带的参数就是在后台任务中传递过来的。 在这个方法中可以对 UI 进行操作,利用参数中的数值就可以对界面元素进行相应地更新。 */@Overrideprotected void onProgressUpdate(Integer... values) {// TODO Auto-generated method stubString text = "" ;if(values[0] == 100) {text = "下载完成" ;}else {text = values[0]+" %" ;}progress.setProgress(values[0]);baifenbi.setText(text);}/** * 后台任务执行完毕并通过 return 语句进行返回时,这个方法就会被调用。 * 返回的数据会作为参数传递到此方法中,可以利用返回的数据来进行一些 UI 操作, 比如说提醒任务执行的结果,以及关闭掉进度条对话框等。 */@Overrideprotected void onPostExecute(Boolean result) {// TODO Auto-generated method stubToast.makeText(MyApplication.getContext(), "进入onPostExecute方法", 1).show();}}}
4、MyApplication.java中的代码:
package com.demo.asynctaskdemo;import android.app.Application;import android.content.Context;public class MyApplication extends Application {private static Context context;@Overridepublic void onCreate() {context = getApplicationContext();}public static Context getContext() {return context;}}
5、AndroidManifest.xml中的代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.demo.asynctaskdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:name="com.demo.asynctaskdemo.MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.demo.asynctaskdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
项目下载:
AsyncTask异步消息处理机制 项目下载
0 0
- AsyncTask异步消息处理机制
- AsyncTask 异步消息处理机制
- 异步消息处理机制-Handler、AsyncTask
- Android AsyncTask异步处理消息机制
- AsyncTask(异步消息处理机制)
- Android笔记----AsyncTask异步消息处理机制
- AsyncTask异步处理机制
- android os;异步消息处理机制:AsyncTask和Handler
- Android异步消息处理机制(3)AsyncTask基本使用
- Android异步消息处理机制(4)AsyncTask源码解析
- Android多线程,异步消息处理机制, Thread, AsyncTask 简单总结
- 安卓开发:AsyncTask异步消息处理机制使用
- 异步消息处理机制及AsyncTask的使用
- 异步消息与AsyncTask机制
- 异步消息处理机制
- 异步消息处理机制
- 异步消息处理机制
- 异步消息处理机制
- 《挑战程序设计竞赛》2.1.2 广度优先搜索 AOJ0558 POJ3669 AOJ0121
- solr时间查询
- VideoView 和 SurfaceView 实例
- UDP协议与TCP/IP协议
- SB 在来盗啊
- AsyncTask 异步消息处理机制
- 数据库对象
- 字符串Hash函数对比
- 最详细的Log4j使用教程(二)
- 5.1、SSAS-入门实例
- Tencent login函数登录成功后无法成功回传数据(无法返回回调方法) Android qq授权登录
- 微信公众平台开发教程目录
- SEO专员,如何评估一个外链的价值?
- 在CodeBlocks环境下使用boost库