一、簡單說明
1, IntentService
IntentService繼承自Service,並在其內部創建了工作線程,用來處理耗時操作,其中onHandleIntent方法就是在子線程執行的,我們可以在這里處理耗時操作。
啟動時與正常service一樣,可以調用startservice來啟動IntentService。
注意:(1),在無參構造函數里,必須調用父類一個參數的構造方法; (2),與普通服務不同,IntentService的子線程處理完耗時操作,服務便銷毀。
2, HandlerThread
HandlerThread 繼承自Thread, 本質上就是一個Thread,內部封裝了Looper,不用我們再處理Looper的初始化與消息循環。
我們可以通過獲取HandlerThread 內部的looper來構建一個子線程的handler,從而與主線程交互。
常見使用方法:
// 1,創建handlerThread並啟動該線程
HandlerThread MyThread = new HandlerThread("MyThread");
MyThread.start();
//2,獲取子線程的looper
Looper looper = MyThread.getLooper();
//3,通過子線程的looper創建handler,從而該handler便與子線程綁定
handler = new Handler(looper);
handler#doSomeThing...
3, AsyncQueryHandler
異步查詢類,常見使用方法:
//1,繼承AsyncQueryHandler並重寫onXXXComplete方法,以便增刪改查等操作執行完畢后進行下一步操作。
private static class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onQueryComplete(token, cookie, cursor);
System.out.println("查詢完畢: " + System.currentTimeMillis());
}
}
//2,調用startXXX方法執行增刪改查操作
Uri uri = Sms.CONTENT_URI;
QueryHandler mQueryHandler = new QueryHandler(getContentResolver());
mQueryHandler.startQuery(100, "cookie", uri, null, null, null, Sms.DEFAULT_SORT_ORDER);
4, Loader
主要用來異步加載數據,常見的有AsyncLoader, CursorLoader,詳情請參考:Android Loader使用詳解
5, AsyncTask
異步任務類,常見,不再贅述
二、代碼Demo
//java code
package com.wytiger.goodclass;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.provider.Telephony.Sms;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
protected static final String TAG = "MainActivity";
private int count = 0;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// 啟動MyIntentService
startService(new Intent(this, MyIntentService.class));
Toast.makeText(this, "點擊IntentService", 0).show();
break;
case R.id.button2:
testHandlerThread();
Toast.makeText(this, "點擊HandlerThread", 0).show();
break;
case R.id.button3:
startQuery();
Toast.makeText(this, "點擊AsyncQueryHandler", 0).show();
break;
default:
break;
}
}
private void testHandlerThread() {
// 創建handlerThread
HandlerThread MyThread = new HandlerThread("MyThread");
MyThread.start();
System.out.println("當前線程:" + Thread.currentThread().getName());
System.out.println("handler線程:" + MyThread.getName());
Looper looper = MyThread.getLooper();
// 子線程的handler
handler = new Handler(looper);
handler.post(mRunnable);
}
/**
* 這在子線程執行
*/
private Runnable mRunnable = new Runnable() {
public void run() {
count++;
// 為了方便 查看,我們用Log打印出來
Log.e(TAG, Thread.currentThread().getName() + ": " + count);
SystemClock.sleep(1000);
// 每2秒執行一次
handler.post(mRunnable);
}
};
@TargetApi(Build.VERSION_CODES.KITKAT)
private void startQuery() {
Uri uri = Sms.CONTENT_URI;
QueryHandler mQueryHandler = new QueryHandler(getContentResolver());
mQueryHandler.startQuery(100, "cookie", uri, null, null, null, Sms.DEFAULT_SORT_ORDER);
System.out.println("開始查詢: " + System.currentTimeMillis());
}
/**
* 異步查詢類
*
* @author wytiger
* @date 2016-5-26
*/
private static class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onQueryComplete(token, cookie, cursor);
System.out.println("查詢完畢: " + System.currentTimeMillis());
}
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
super.onInsertComplete(token, cookie, uri);
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
super.onUpdateComplete(token, cookie, result);
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {
super.onDeleteComplete(token, cookie, result);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(mRunnable);
}
}
//xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IntentService" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HandlerThread" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AsyncQueryHandler" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loader" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AsyncTask" />
</LinearLayout>
//權限聲明
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />