异步任务——AsyncTask的初步认识-LMLPHP

异步任务——AsyncTask的初步认识-LMLPHP

异步任务——AsyncTask的初步认识-LMLPHP

异步任务——AsyncTask的初步认识-LMLPHP

异步任务——AsyncTask的初步认识-LMLPHP

ProgressBar_test.class
package com.example.administrator.ten_9;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView; /**
* Created by Administrator on 2015/10/9 0009.
*/
public class ProgressBar_test extends Activity {
private ProgressBar progressBar;
private my my;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
progressBar = (ProgressBar) findViewById(R.id.pb);
textView = (TextView) findViewById(R.id.textjindu);
my = new my();
my.execute();
} @Override
protected void onPause() {
super.onPause();
//生命周期绑定
//AsyncTask不为空而且是在Running的
if (my != null && my.getStatus() == AsyncTask.Status.RUNNING){
//cancel方法只是将对应的AsyncTask标记为cancel状态,并不是真正的取消线程执行。
my.cancel(true);
}
} class my extends AsyncTask<Void,Integer,Void>{ @Override
protected Void doInBackground(Void... params) {
//模拟进度更新
for(int i = 0; i <= 100 ; i++){
//判断是否被标记为 cancel 是 就break出来
if(isCancelled()){
break;
}
publishProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} return null;
} @Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.d("xys", String.valueOf(values[0]));
//判断是否被标记为 cancel 是 就直接返回
if(isCancelled()){
return;
}
//获取更新进度
textView.setText(values[0]+"");
progressBar.setProgress(values[0]);
}
}
}

  

progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar
android:layout_centerInParent="true"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pb" />
<TextView
android:id="@+id/textjindu"
android:gravity="center_horizontal"
android:textSize="20sp"
android:text="0"
android:layout_below="@id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </RelativeLayout>

  

MainActivity.class
public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); MyAsyncTask myAsyncTask = new MyAsyncTask();
//start 异步处理myAsyncTask
myAsyncTask.execute();
} public void LoadImage(View view){
startActivity(new Intent(this,ImageTest.class)); }
public void LoadProgress(View view){
startActivity(new Intent(this,ProgressBar_test.class));
}
}

  mainactivity.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button
android:id="@+id/bt1"
android:text="跳转"
android:onClick="LoadImage"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_below="@id/bt1"
android:id="@+id/bt2"
android:text="跳转2"
android:onClick="LoadImage"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </RelativeLayout>

  

05-26 22:32