为简单而努力:Android封装类详解
一、简单说明
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" />