我们都知道,Android的主线程不能做耗时处理,而子线程又不能修改主线程的UI组件;如果每次想要子线程通过耗时处理后再发送消息给主线程,让其修改自身的UI组件,显得比较繁琐;所以Android为我们提供了一个AsyncTask的类方便我们操作。
大多数时候,我们总能遇到这样的情款,当浏览从网络发来的数据时,文字加载的快,而图片加载的慢,所以不可能让全部加载完再显示给读者吧;因而可以将图片加载的用该类来实现。
当然今天不是讲加载图片,只是举给简单的例子:用该类修改Activity中的textview显示数字而已(0~99的变化),^_^,一下子level低了好多;
直接附上源码,注释我已经写在里面;
MainActivity.java:
package com.myproject.wyc.myapp0312;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.wyc.myclass.MyAsyThread;
public class MainActivity extends ActionBarActivity {
private String TAG = "MyTag";
private TextView tvObj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvObj = (TextView) findViewById(R.id.tvid);
MyAsyThread myAsyThread = new MyAsyThread();
myAsyThread.execute(tvObj);//相当于Thread.start(传入参数);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyAsyThread.java:
package com.wyc.myclass;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;
/**
* Created by Administrator on 15-3-12.
*/
public class MyAsyThread extends AsyncTask<TextView,Integer,Integer> {
private String TAG = "MyTag";
private Boolean aBoolean = false;
private int count;
private TextView tvObj;
/**
* doInBackground()是异步类执行的主体;
* 在子线程中,因此它是不能操作主线程ui组件;
* 如果在操作ui组件,可以在此方法中调用publishProgress(Progress...)方法完成;
* @param params 传入参数数组
* @return 返回值将作为onPostExecute(Integer integer)的参数;
*/
@Override
protected Integer doInBackground(TextView... params) {
Log.i(TAG,"doInBackground");
tvObj = params[0];
int count = 0;
aBoolean = true;
if(aBoolean) {
for (int i=0; i < 100; i++) {
publishProgress(count++);
SystemClock.sleep(1000);
}
}
return params.length;
}
/**
* doInBackground方法中调用publishProgress,会触发该方法;
* 在该方法中操作UI组件,
* @param values 是调用时传入的参数,也是一个数组,所以取值时,写values[0]
*/
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i(TAG,"onProgressUpdate");
tvObj.setText(String.valueOf(values[0]));
}
/**
*后台任务取消,就会触发,因为在java中线程一旦创建就不会销毁,
* 所以想要使其不工作,则将标志位设置为false,具体可以根据自己的需求而定;
*/
@Override
protected void onCancelled() {
super.onCancelled();
Log.i(TAG,"onCancelled");
aBoolean = false;
}
/**
* doInBackground方法返回后,会调用该方法,
* 并将 doInBackground方法的返回值作为该方法的参数;
* @param integer doInBackground方法的返回值;
*/
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
Log.i(TAG,"onPostExecute");
}
}
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" 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">
<TextView
android:id="@+id/tvid"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>