package com.artifex.mupdfdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class L_Home extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.l_home);
tv = (TextView) findViewById(R.id.textView1);
DownloadTask dTask = new DownloadTask();
dTask.execute(10);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.e("onStart", "onStart");
super.onStart();
}
class DownloadTask extends AsyncTask<Integer, Integer, String> {
// 後面尖括號內分別的參數是 傳入doInBackground、進度(publishProgress用到)、返回onPostExecute值類型
@Override
protected void onPreExecute() {
// 第一個執行方法
super.onPreExecute();
}
@Override
protected String doInBackground(Integer... params) {
// 第二個執行方法,onPreExecute()執行完後執行
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(params[0]);
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "執行完畢";
}
@Override
protected void onPostExecute(String result) {
// doInBackground返回時觸發,換句話說,就是doInBackground執行完後觸發
// 這裡的result就是上面doInBackground執行後的返回值,所以這裡是"執行完畢"
super.onPostExecute(result);
// Toast.makeText(L_Home.this, result, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(L_Home.this, L_TabActivity.class);
startActivity(intent);
finish();
}
@Override
protected void onProgressUpdate(Integer... progress) {
// 這個函數在doInBackground調用publishProgress時觸發,雖然調用時只有一個參數
// 但是這裡取到的是一個數組,所以要用progesss[0]來取值
// 第n個參數就用progress[n]來取值
tv.setText(progress[0] + "%");
super.onProgressUpdate(progress);
}
}
}