android.content.AsyncQueryHandler继续于android.os.Handler
虽然Handler自己是没另开新线程,但是
AsyncQueryHandler自己却是另开了一个线程来进行数据的查询
实例1
AsyncQueryHandler -->同步更新
class QueryHandler extends AsyncQueryHandler {
QueryHandler(ContentResolver res) {
super(res);//[这里让AsyncQueryHandler和ContentResolver联系起来]
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor)//监听QueryComplete事件
{
mActivity.init(cursor);
}
}
AsyncQueryHandler--->当contentProvied发生变化时候同步更新显示就可以通过使用AsyncQueryHandler类来达到这一要求
上面的代码--->onQueryComplete()--->就是当cursor更新完之后的
public void init(Cursor c) {
mAdapter.changeCursor(c);
if (mQueryCursor == null) {
MusicUtils.displayDatabaseError(this);
setListAdapter(null);
mReScanHandler.sendEmptyMessageDelayed(0, 1000);
}
MusicUtils.hideDatabaseError(this);
}
实例2:
package com.android.phone;
import static android.view.Window.PROGRESS_VISIBILITY_OFF;
import static android.view.Window.PROGRESS_VISIBILITY_ON;
import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
/**
* ADN List activity for the Phone app.
*/
public class ADNList extends ListActivity {
protected static final String TAG = "ADNList";
protected static final boolean DBG = false;
private static final String[] COLUMN_NAMES = new String[] {
"name",
"number"
};
protected static final int NAME_COLUMN = 0;
protected static final int NUMBER_COLUMN = 1;
private static final int[] VIEW_NAMES = new int[] {
android.R.id.text1,
android.R.id.text2
};
protected static final int QUERY_TOKEN = 0;
protected static final int INSERT_TOKEN = 1;
protected static final int UPDATE_TOKEN = 2;
protected static final int DELETE_TOKEN = 3;
protected QueryHandler mQueryHandler;
protected CursorAdapter mCursorAdapter;
protected Cursor mCursor = null;
private TextView mEmptyText;
protected int mInitialSelection = -1;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.adn_list);
mEmptyText = (TextView) findViewById(android.R.id.empty);
mQueryHandler = new QueryHandler(getContentResolver());
}
@Override
protected void onResume() {
super.onResume();
query();
}
@Override
protected void onStop() {
super.onStop();
if (mCursor != null) {
mCursor.deactivate();
}
}
protected Uri resolveIntent() {
Intent intent = getIntent();
if (intent.getData() == null) {
intent.setData(Uri.parse("content://icc/adn"));
}
return intent.getData();
}
private void query() {
Uri uri = resolveIntent();
if (DBG) log("query: starting an async query");
mQueryHandler.startQuery(QUERY_TOKEN, null, uri, COLUMN_NAMES,
null, null, null);
displayProgress(true);
}
private void reQuery() {
query();
}
private void setAdapter() {
if (mCursorAdapter == null) {
mCursorAdapter = newAdapter();
setListAdapter(mCursorAdapter);
} else {
mCursorAdapter.changeCursor(mCursor);
}
if (mInitialSelection >=0 && mInitialSelection < mCursorAdapter.getCount()) {
setSelection(mInitialSelection);
getListView().setFocusableInTouchMode(true);
boolean gotfocus = getListView().requestFocus();
}
}
protected CursorAdapter newAdapter() {
return new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
mCursor, COLUMN_NAMES, VIEW_NAMES);
}
private void displayProgress(boolean flag) {
if (DBG) log("displayProgress: " + flag);
mEmptyText.setText(flag ? R.string.simContacts_emptyLoading: R.string.simContacts_empty);
getWindow().setFeatureInt(
Window.FEATURE_INDETERMINATE_PROGRESS,
flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
}
private class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor c) {//监听QueryComplete事件
if (DBG) log("onQueryComplete: cursor.count=" + c.getCount());
mCursor = c;
setAdapter();
displayProgress(false);
?
@Override
protected void onInsertComplete(int token, Object cookie,//监听InsertComplete事件
Uri uri) {
if (DBG) log("onInsertComplete: requery");
reQuery();
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {//监听onUpdateComplete事件
if (DBG) log("onUpdateComplete: requery");
reQuery();
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {//监听DeleteComplete事件
if (DBG) log("onDeleteComplete: requery");
reQuery();
}
}
protected void log(String msg) {
Log.d(TAG, "[ADNList] " + msg);
}
}
实例1
AsyncQueryHandler -->同步更新
class QueryHandler extends AsyncQueryHandler {
QueryHandler(ContentResolver res) {
super(res);//[这里让AsyncQueryHandler和ContentResolver联系起来]
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor)//监听QueryComplete事件
{
mActivity.init(cursor);
}
}
AsyncQueryHandler--->当contentProvied发生变化时候同步更新显示就可以通过使用AsyncQueryHandler类来达到这一要求
上面的代码--->onQueryComplete()--->就是当cursor更新完之后的
public void init(Cursor c) {
mAdapter.changeCursor(c);
if (mQueryCursor == null) {
MusicUtils.displayDatabaseError(this);
setListAdapter(null);
mReScanHandler.sendEmptyMessageDelayed(0, 1000);
}
MusicUtils.hideDatabaseError(this);
}
实例2:
package com.android.phone;
import static android.view.Window.PROGRESS_VISIBILITY_OFF;
import static android.view.Window.PROGRESS_VISIBILITY_ON;
import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
/**
* ADN List activity for the Phone app.
*/
public class ADNList extends ListActivity {
protected static final String TAG = "ADNList";
protected static final boolean DBG = false;
private static final String[] COLUMN_NAMES = new String[] {
"name",
"number"
};
protected static final int NAME_COLUMN = 0;
protected static final int NUMBER_COLUMN = 1;
private static final int[] VIEW_NAMES = new int[] {
android.R.id.text1,
android.R.id.text2
};
protected static final int QUERY_TOKEN = 0;
protected static final int INSERT_TOKEN = 1;
protected static final int UPDATE_TOKEN = 2;
protected static final int DELETE_TOKEN = 3;
protected QueryHandler mQueryHandler;
protected CursorAdapter mCursorAdapter;
protected Cursor mCursor = null;
private TextView mEmptyText;
protected int mInitialSelection = -1;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.adn_list);
mEmptyText = (TextView) findViewById(android.R.id.empty);
mQueryHandler = new QueryHandler(getContentResolver());
}
@Override
protected void onResume() {
super.onResume();
query();
}
@Override
protected void onStop() {
super.onStop();
if (mCursor != null) {
mCursor.deactivate();
}
}
protected Uri resolveIntent() {
Intent intent = getIntent();
if (intent.getData() == null) {
intent.setData(Uri.parse("content://icc/adn"));
}
return intent.getData();
}
private void query() {
Uri uri = resolveIntent();
if (DBG) log("query: starting an async query");
mQueryHandler.startQuery(QUERY_TOKEN, null, uri, COLUMN_NAMES,
null, null, null);
displayProgress(true);
}
private void reQuery() {
query();
}
private void setAdapter() {
if (mCursorAdapter == null) {
mCursorAdapter = newAdapter();
setListAdapter(mCursorAdapter);
} else {
mCursorAdapter.changeCursor(mCursor);
}
if (mInitialSelection >=0 && mInitialSelection < mCursorAdapter.getCount()) {
setSelection(mInitialSelection);
getListView().setFocusableInTouchMode(true);
boolean gotfocus = getListView().requestFocus();
}
}
protected CursorAdapter newAdapter() {
return new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
mCursor, COLUMN_NAMES, VIEW_NAMES);
}
private void displayProgress(boolean flag) {
if (DBG) log("displayProgress: " + flag);
mEmptyText.setText(flag ? R.string.simContacts_emptyLoading: R.string.simContacts_empty);
getWindow().setFeatureInt(
Window.FEATURE_INDETERMINATE_PROGRESS,
flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
}
private class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor c) {//监听QueryComplete事件
if (DBG) log("onQueryComplete: cursor.count=" + c.getCount());
mCursor = c;
setAdapter();
displayProgress(false);
?
@Override
protected void onInsertComplete(int token, Object cookie,//监听InsertComplete事件
Uri uri) {
if (DBG) log("onInsertComplete: requery");
reQuery();
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {//监听onUpdateComplete事件
if (DBG) log("onUpdateComplete: requery");
reQuery();
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {//监听DeleteComplete事件
if (DBG) log("onDeleteComplete: requery");
reQuery();
}
}
protected void log(String msg) {
Log.d(TAG, "[ADNList] " + msg);
}
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow