Android中的AsyncTask异步任务的简单实例
生活随笔
收集整理的這篇文章主要介紹了
Android中的AsyncTask异步任务的简单实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在?
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?討論
總結
以上是生活随笔為你收集整理的Android中的AsyncTask异步任务的简单实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中的AsyncTask异步
- 下一篇: Android中TextView限制一行