本文来自网易云社区
作者:孙有军
在前一篇文章中我们探究了AsyncTask的执行流程,那这一篇里面就写一个小的demo,来尝试一下AsyncTask的调用。
1:布局文件:
<LinearLayout 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:orientation="vertical"
tools:context="com.example.toolbox.suspend.SuspendActivity" >
<TextView
android:id="@+id/async_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:background="@drawable/g_green_btn_selector"
android:gravity="center"
android:padding="10dip"
android:text="@string/async_task"
android:textColor="@color/white_color"
android:textSize="16sp" />
<TextView
android:id="@+id/async_task_thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:background="@drawable/g_green_btn_selector"
android:gravity="center"
android:padding="10dip"
android:text="@string/async_task_thread"
android:textColor="@color/white_color"
android:textSize="16sp" />
<TextView
android:id="@+id/async_task_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:background="@drawable/g_green_btn_selector"
android:gravity="center"
android:padding="10dip"
android:text="@string/async_task_result"
android:textColor="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
public class AsyncTaskActivity extends ActionBarActivity implements OnClickListener {
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.async_task_activity);
findViews();
setViewListener();
}
private void findViews() {
result = (TextView) findViewById(R.id.async_task_result);
}
private void setViewListener() {
findViewById(R.id.async_task).setOnClickListener(this);
findViewById(R.id.async_task_thread).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.async_task:
startAsyncTask(3);
break;
case R.id.async_task_thread:
startAsyncTask2();
default:
break;
}
}
private void startAsyncTask2() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
startAsyncTask(5);
}
});
thread.start();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB) private void startAsyncTask(int value) {
AsyncTask<Integer, Void, Integer> task = new AsyncTask<Integer, Void, Integer>() {
@Override
protected Integer doInBackground(Integer... params) {
return params[0] * params[0];
}
@Override
protected void onPostExecute(Integer result) {
Toast.makeText(AsyncTaskActivity.this, "Current Value = " + result, Toast.LENGTH_LONG).show();
setTextValue("Current Value =" + result);
}
};
if (Build.VERSION.SDK_INT >= 11) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,value);
} else {
task.execute(value);
}
}
private void setTextValue(String text) {
result.setText(text);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
public static void init() {
sHandler.getLooper();
}
这个Looper就是MainLooper,但是init这个函数是在什么地方调用的呐?如果是MainLooper那应该就是在UI线程中调用的,那又在UI线程的什么地方进行调用的呐?
public static void main(String[] args) {
5107 SamplingProfilerIntegration.start();
5108
5109 // CloseGuard defaults to true and can be quite spammy. We
5110 // disable it here, but selectively enable it later (via
5111 // StrictMode) on debug builds, but using DropBox, not logs.
5112 CloseGuard.setEnabled(false);
5113
5114 Environment.initForCurrentUser();
5115
5116 // Set the reporter for event logging in libcore
5117 EventLogger.setReporter(new EventLoggingReporter());
5118
5119 Security.addProvider(new AndroidKeyStoreProvider());
5120
5121 Process.setArgV0("<pre-initialized>");
5122
5123 Looper.prepareMainLooper();
5124
5125 ActivityThread thread = new ActivityThread();
5126 thread.attach(false);
5127
5128 if (sMainThreadHandler == null) {
5129 sMainThreadHandler = thread.getHandler();
5130 }
5131
5132 AsyncTask.init();
5133
5134 if (false) {
5135 Looper.myLooper().setMessageLogging(new
5136 LogPrinter(Log.DEBUG, "ActivityThread"));
5137 }
5138
5139 Looper.loop();
5140
5141 throw new RuntimeException("Main thread loop unexpectedly exited");
5142 }
5143}