http://blog.csdn.net/hfreeman2011/article/details/8555474
AsyncQueryHandler:
官方解释是一个异步帮助类(A helper class to help make handling asynchronous ContentResolver
queries easier.) 。这个类的主要作用就是异步对DB数据库进行操作,加快其数据处理的速度(这个非常重要,特别是大容量的数据处理时,例如几千联系人的数据读取,按正常的处理速度会非常的慢,使用AsyncQueryHandler,这就会大大的加快速度,增加用户的良好体验)。
AsyncQueryHandler重要的方法:
final void |
startDelete(int token,
Object cookie,
Uri uri,
String selection,
String[] selectionArgs)
This method begins an asynchronous delete.
|
final void |
startInsert(int token,
Object cookie,
Uri uri,
ContentValues initialValues)
This method begins an asynchronous insert.
|
void |
startQuery(int token,
Object cookie,
Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String orderBy)
This method begins an asynchronous query.
|
final void |
startUpdate(int token,
Object cookie,
Uri uri,
ContentValues values,
String selection,
String[] selectionArgs)
This method begins an asynchronous update.
|
void |
onDeleteComplete(int token,
Object cookie, int result)
Called when an asynchronous delete is completed.
|
void |
onInsertComplete(int token,
Object cookie,
Uri uri)
Called when an asynchronous insert is completed.
|
void |
onQueryComplete(int token,
Object cookie,
Cursor cursor)
Called when an asynchronous query is completed.
|
void |
onUpdateComplete(int token,
Object cookie, int result)
Called when an asynchronous update is completed.
|
AsyncQueryHandler的一个应用:读取电话联系人的数据信息
- public class TestAsyncQueryHandler extends Activity {
- private static final String NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";
- private List<ContentValues> listData;
- private AsyncQueryHandler asyncQuery;
- private ListView personList;
- private BaseAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- personList = (ListView) findViewById(R.id.list_view);
- asyncQuery = new MyAsyncQueryHandler(getContentResolver());
- //异步读取联系人的信息
- asyncQueryContact();
- }
- private void asyncQueryContact() {
- // TODO Auto-generated method stub
- Uri uri = Uri.parse("content://com.android.contacts/data/phones");
- String[] projection = { "_id", "display_name", "data1", "sort_key" };
- asyncQuery.startQuery(0, null, uri, projection, null, null,"sort_key COLLATE LOCALIZED asc");
- }
- private class MyAsyncQueryHandler extends AsyncQueryHandler {
- public MyAsyncQueryHandler(ContentResolver cr) {
- super(cr);
- }
- @Override
- protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
- if (cursor != null && cursor.getCount() > 0) {
- listData = new ArrayList<ContentValues>();
- //cursor.moveToFirst();
- for (int i = 0; i < cursor.getCount(); i++) {
- ContentValues cv = new ContentValues();
- cursor.moveToPosition(i);
- String name = cursor.getString(1);
- String number = cursor.getString(2);
- String sortKey = cursor.getString(3);
- if (number.startsWith("+86")) {
- cv.put(NAME, name);
- //process (+86)
- cv.put(NUMBER, number.substring(3));
- cv.put(SORT_KEY, sortKey);
- } else {
- cv.put(NAME, name);
- cv.put(NUMBER, number);
- cv.put(SORT_KEY, sortKey);
- }
- listData.add(cv);
- }
- if (listData.size() > 0) {
- setAdapter(listData);
- }
- cursor.close();
- }
- }
- }
- private void setAdapter(List<ContentValues> listData) {
- adapter = new ListAdapter(this, listData);
- personList.setAdapter(adapter);
- }
- private class ListAdapter extends BaseAdapter {
- private LayoutInflater inflater;
- private List<ContentValues> list;
- public ListAdapter(Context context, List<ContentValues> list) {
- this.inflater = LayoutInflater.from(context);
- this.list = list;
- }
- @Override
- public int getCount() {
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- return list.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- convertView = inflater.inflate(R.layout.list_item, null);
- holder = new ViewHolder();
- holder.name = (TextView) convertView.findViewById(R.id.name);
- holder.number = (TextView) convertView.findViewById(R.id.number);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- ContentValues cv = list.get(position);
- holder.name.setText(cv.getAsString(NAME));
- holder.number.setText(cv.getAsString(NUMBER));
- return convertView;
- }
- private class ViewHolder {
- TextView name;
- TextView number;
- }
- }
- }
这个例子,只是使用了AsyncQueryHandler类中的异步查询方法,其它的方法,也是类似,大家可以自己尝试。
源代码下载地址:
http://download.csdn.net/detail/hfreeman2011/5040647
【安卓笔记】AsyncQueryHandler源码剖析
http://www.th7.cn/Program/Android/201409/275333.shtml
AsyncQueryHandler类用于提供异步数据库操作(增删改查)功能。本文视图从源码角度分析这个类。
1.AsyncQueryHandler 使用方式:
AsyncQueryHandler handler = new AsyncQueryHandler(this.getContentResolver()) { @Override protected void onQueryComplete(int token, Object cookie,Cursor cursor) { } @Override protected void onUpdateComplete(int token, Object cookie, int result) { } @Override protected void onInsertComplete(int token, Object cookie, Uri uri) { } @Override protected void onDeleteComplete(int token, Object cookie, int result) { } }; handler.startQuery(token, cookie, uri, projection, selection, selectionArgs, orderBy); handler.startDelete(token, cookie, uri, selection, selectionArgs); handler.startInsert(token, cookie, uri, initialValues); handler.startUpdate(token, cookie, uri, values, selection, selectionArgs);
2.源码分析
分析源码前,从整体上把握这个类:每个AsyncQueryHandler对象都会开启一个后台线程,在线程中执行与ContentProvider组件的数据交互,进行增删改查。调用时,可以通过AsyncQueryHandler.startXXX系列方法将请求打包发送到后台线程,当相关处理完成后,会将结果异步回传给主线程并调用AsyncQueryHandler.onXXXComplete方法通知调用者。调用者每次请求时,需要传入一个整型值token作为这次请求的标识,当该请求完成后进行回调时,会将token传回,帮助调用者确定这是哪一次请求。
http://blog.csdn.net/t12x3456/article/details/7912805
Android 异步查询框架AsyncQueryHandler的使用
AsyncQueryHandler简介:
异步的查询操作帮助类,可以处理增删改(ContentProvider提供的数据)
使用场景:
在一般的应用中可以使用ContentProvider去操作数据库。
这在数据量很小的时候是没有问题的,但是如果数据量大了,可能导致UI线程发生ANR异常(超过5秒)。
当然你也可以写个Handler去做这些操作,只是你每次使用ContentProvider时都要再写个Handler,必然降低了效率。
因此当数据量较大时,最好还是使用Android已经封装好的异步查询框架AsyncQueryHandler,优化我们的代码.
要注意的是,一般在查询本地的应用的数据的时候要去采用CursorAdapter。
AsyncQueryHandler内部实现
AsyncQueryHandler类封装了调用者线程与工作线程的交互过程。交互的主体是两个Handler,一个运行在调用者线程中,一个运行在工作者线程中。通过提供onXXXComplete的回调接口,实现事件的完成处理。
API中提供
startQuery四种方法,并有响应的onXXXComplete()方法.于对应的4个onXXXComplete()方法都是空实现,因此我们完成相应调用后进行后续其他的操作.
使用方法
继承AsyncQueryHandler类,并提供onXXXComplete方法的实现(可以实现任何一个或多个,当然你也可以一个也不实现,如果你不关注操作数据库的結果),在你的实现中做一些对数据库操作完成的处理。
使用时直接调用startXXX方法即可。传入的通用参数如下:
token,一个令牌,主要用来标识查询,保证唯一即可.需要跟onXXXComplete方法传入的一致。(当然你也可以不一致,同样在数据库的操作结束后会调用对应的onXXXComplete方法 )
cookie,你想传给onXXXComplete方法使用的一个对象。(没有的话传递null即可)
Uri uri(进行查询的通用资源标志符):
projection 查询的列
selection 限制条件
selectionArgs 查询参数
ANR:
ANR(Application Not Responding)定义
在Android上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对话框。用户可以选择“等待”而让程序继续运行,也可以选择“强制关闭”。所以一个流畅的合理的应用程序中不能出现anr,而让用户每次都要处理这个对话框。因此,在程序里对响应性能的设计很重要,这样系统不会显示ANR给用户。默认情况下,在android中Activity的最长执行时间是5秒,BroadcastReceiver的最长执行时间则是10秒。