android.content.AsyncQueryHandler继续于android.os.Handler
虽然Handler自己是没另开新线程,但是AsyncQueryHandler自己却是另开了一个线程来进行数据的查询
<wbr style="line-height:25px"><span style="line-height:25px">实例1</span><br style="line-height:25px"> AsyncQueryHandler-->同步更新<br style="line-height:25px"><br style="line-height:25px"> classQueryHandlerextendsAsyncQueryHandler{<br style="line-height:25px"> QueryHandler(ContentResolverres){<br style="line-height:25px"> super(res);//[这里让AsyncQueryHandler和ContentResolver联系起来]<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonQueryComplete(inttoken,Objectcookie,Cursorcursor)//监听QueryComplete事件<br style="line-height:25px"> {<br style="line-height:25px"> mActivity.init(cursor);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> AsyncQueryHandler--->当contentProvied发生变化时候同步更新显示就可以通过使用AsyncQueryHandler类来达到这一要求<br style="line-height:25px"><br style="line-height:25px"> 上面的代码--->onQueryComplete()--->就是当cursor更新完之后的<br style="line-height:25px"> publicvoidinit(Cursorc){<br style="line-height:25px"> mAdapter.changeCursor(c);<br style="line-height:25px"> if(mQueryCursor==null){<br style="line-height:25px"> MusicUtils.displayDatabaseError(this);<br style="line-height:25px"> setListAdapter(null);<br style="line-height:25px"> mReScanHandler.sendEmptyMessageDelayed(0,1000);<br style="line-height:25px"> }<br style="line-height:25px"> MusicUtils.hideDatabaseError(this);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> 实例2:<br style="line-height:25px"> packagecom.android.phone;<br style="line-height:25px"><br style="line-height:25px"> importstaticandroid.view.Window.PROGRESS_VISIBILITY_OFF;<br style="line-height:25px"> importstaticandroid.view.Window.PROGRESS_VISIBILITY_ON;<br style="line-height:25px"><br style="line-height:25px"> importandroid.app.ListActivity;<br style="line-height:25px"> importandroid.content.AsyncQueryHandler;<br style="line-height:25px"> importandroid.content.ContentResolver;<br style="line-height:25px"> importandroid.content.Intent;<br style="line-height:25px"> importandroid.database.Cursor;<br style="line-height:25px"> importandroid.net.Uri;<br style="line-height:25px"> importandroid.os.Bundle;<br style="line-height:25px"> importandroid.util.Log;<br style="line-height:25px"> importandroid.view.Window;<br style="line-height:25px"> importandroid.widget.CursorAdapter;<br style="line-height:25px"> importandroid.widget.SimpleCursorAdapter;<br style="line-height:25px"> importandroid.widget.TextView;<br style="line-height:25px"><br style="line-height:25px"> /**<br style="line-height:25px"> *ADNListactivityforthePhoneapp.<br style="line-height:25px"> */<br style="line-height:25px"> publicclassADNListextendsListActivity{<br style="line-height:25px"> protectedstaticfinalStringTAG="ADNList";<br style="line-height:25px"> protectedstaticfinalbooleanDBG=false;<br style="line-height:25px"><br style="line-height:25px"> privatestaticfinalString[]COLUMN_NAMES=newString[]{<br style="line-height:25px"> "name",<br style="line-height:25px"> "number"<br style="line-height:25px"> };<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> protectedstaticfinalintNAME_COLUMN=0;<br style="line-height:25px"> protectedstaticfinalintNUMBER_COLUMN=1;<br style="line-height:25px"><br style="line-height:25px"> privatestaticfinalint[]VIEW_NAMES=newint[]{<br style="line-height:25px"> android.R.id.text1,<br style="line-height:25px"> android.R.id.text2<br style="line-height:25px"> };<br style="line-height:25px"><br style="line-height:25px"> protectedstaticfinalintQUERY_TOKEN=0;<br style="line-height:25px"> protectedstaticfinalintINSERT_TOKEN=1;<br style="line-height:25px"> protectedstaticfinalintUPDATE_TOKEN=2;<br style="line-height:25px"> protectedstaticfinalintDELETE_TOKEN=3;<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> protectedQueryHandlermQueryHandler;<br style="line-height:25px"> protectedCursorAdaptermCursorAdapter;<br style="line-height:25px"> protectedCursormCursor=null;<br style="line-height:25px"><br style="line-height:25px"> privateTextViewmEmptyText;<br style="line-height:25px"><br style="line-height:25px"> protectedintmInitialSelection=-1;<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonCreate(Bundleicicle){<br style="line-height:25px"> super.onCreate(icicle);<br style="line-height:25px"> getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);<br style="line-height:25px"> setContentView(R.layout.adn_list);<br style="line-height:25px"> mEmptyText=(TextView)findViewById(android.R.id.empty);<br style="line-height:25px"> mQueryHandler=newQueryHandler(getContentResolver());<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonResume(){<br style="line-height:25px"> super.onResume();<br style="line-height:25px"> query();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonStop(){<br style="line-height:25px"> super.onStop();<br style="line-height:25px"> if(mCursor!=null){<br style="line-height:25px"> mCursor.deactivate();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> protectedUriresolveIntent(){<br style="line-height:25px"> Intentintent=getIntent();<br style="line-height:25px"> if(intent.getData()==null){<br style="line-height:25px"> intent.setData(Uri.parse("content://icc/adn"));<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> returnintent.getData();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidquery(){<br style="line-height:25px"> Uriuri=resolveIntent();<br style="line-height:25px"> if(DBG)log("query:startinganasyncquery");<br style="line-height:25px"> mQueryHandler.startQuery(QUERY_TOKEN,null,uri,COLUMN_NAMES,<br style="line-height:25px"> null,null,null);<br style="line-height:25px"> displayProgress(true);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidreQuery(){<br style="line-height:25px"> query();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidsetAdapter(){<br style="line-height:25px"><br style="line-height:25px"> if(mCursorAdapter==null){<br style="line-height:25px"> mCursorAdapter=newAdapter();<br style="line-height:25px"><br style="line-height:25px"> setListAdapter(mCursorAdapter);<br style="line-height:25px"> }else{<br style="line-height:25px"> mCursorAdapter.changeCursor(mCursor);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> if(mInitialSelection>=0&&mInitialSelection<mCursorAdapter.getCount()){<br style="line-height:25px"> setSelection(mInitialSelection);<br style="line-height:25px"> getListView().setFocusableInTouchMode(true);<br style="line-height:25px"> booleangotfocus=getListView().requestFocus();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> protectedCursorAdapternewAdapter(){<br style="line-height:25px"> returnnewSimpleCursorAdapter(this,<br style="line-height:25px"> android.R.layout.simple_list_item_2,<br style="line-height:25px"> mCursor,COLUMN_NAMES,VIEW_NAMES);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoiddisplayProgress(booleanflag){<br style="line-height:25px"> if(DBG)log("displayProgress:"+flag);<br style="line-height:25px"> mEmptyText.setText(flag?R.string.simContacts_emptyLoading:R.string.simContacts_empty);<br style="line-height:25px"> getWindow().setFeatureInt(<br style="line-height:25px"> Window.FEATURE_INDETERMINATE_PROGRESS,<br style="line-height:25px"> flag?PROGRESS_VISIBILITY_ON:PROGRESS_VISIBILITY_OFF);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privateclassQueryHandlerextendsAsyncQueryHandler{<br style="line-height:25px"> publicQueryHandler(ContentResolvercr){<br style="line-height:25px"> super(cr);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonQueryComplete(inttoken,Objectcookie,Cursorc){//监听QueryComplete事件<br style="line-height:25px"> if(DBG)log("onQueryComplete:cursor.count="+c.getCount());<br style="line-height:25px"> mCursor=c;<br style="line-height:25px"> setAdapter();<br style="line-height:25px"> displayProgress(false);<br style="line-height:25px"> ?<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonInsertComplete(inttoken,Objectcookie,//监听InsertComplete事件<br style="line-height:25px"> Uriuri){<br style="line-height:25px"> if(DBG)log("onInsertComplete:requery");<br style="line-height:25px"> reQuery();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonUpdateComplete(inttoken,Objectcookie,intresult){//监听onUpdateComplete事件<br style="line-height:25px"> if(DBG)log("onUpdateComplete:requery");<br style="line-height:25px"> reQuery();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonDeleteComplete(inttoken,Objectcookie,intresult){//监听DeleteComplete事件<br style="line-height:25px"> if(DBG)log("onDeleteComplete:requery");<br style="line-height:25px"> reQuery();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> protectedvoidlog(Stringmsg){<br style="line-height:25px"> Log.d(TAG,"[ADNList]"+msg);<br style="line-height:25px"> }<br style="line-height:25px"> }</wbr>
<wbr style="line-height:25px"><span style="line-height:25px">实例1</span><br style="line-height:25px"> AsyncQueryHandler-->同步更新<br style="line-height:25px"><br style="line-height:25px"> classQueryHandlerextendsAsyncQueryHandler{<br style="line-height:25px"> QueryHandler(ContentResolverres){<br style="line-height:25px"> super(res);//[这里让AsyncQueryHandler和ContentResolver联系起来]<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonQueryComplete(inttoken,Objectcookie,Cursorcursor)//监听QueryComplete事件<br style="line-height:25px"> {<br style="line-height:25px"> mActivity.init(cursor);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> AsyncQueryHandler--->当contentProvied发生变化时候同步更新显示就可以通过使用AsyncQueryHandler类来达到这一要求<br style="line-height:25px"><br style="line-height:25px"> 上面的代码--->onQueryComplete()--->就是当cursor更新完之后的<br style="line-height:25px"> publicvoidinit(Cursorc){<br style="line-height:25px"> mAdapter.changeCursor(c);<br style="line-height:25px"> if(mQueryCursor==null){<br style="line-height:25px"> MusicUtils.displayDatabaseError(this);<br style="line-height:25px"> setListAdapter(null);<br style="line-height:25px"> mReScanHandler.sendEmptyMessageDelayed(0,1000);<br style="line-height:25px"> }<br style="line-height:25px"> MusicUtils.hideDatabaseError(this);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> 实例2:<br style="line-height:25px"> packagecom.android.phone;<br style="line-height:25px"><br style="line-height:25px"> importstaticandroid.view.Window.PROGRESS_VISIBILITY_OFF;<br style="line-height:25px"> importstaticandroid.view.Window.PROGRESS_VISIBILITY_ON;<br style="line-height:25px"><br style="line-height:25px"> importandroid.app.ListActivity;<br style="line-height:25px"> importandroid.content.AsyncQueryHandler;<br style="line-height:25px"> importandroid.content.ContentResolver;<br style="line-height:25px"> importandroid.content.Intent;<br style="line-height:25px"> importandroid.database.Cursor;<br style="line-height:25px"> importandroid.net.Uri;<br style="line-height:25px"> importandroid.os.Bundle;<br style="line-height:25px"> importandroid.util.Log;<br style="line-height:25px"> importandroid.view.Window;<br style="line-height:25px"> importandroid.widget.CursorAdapter;<br style="line-height:25px"> importandroid.widget.SimpleCursorAdapter;<br style="line-height:25px"> importandroid.widget.TextView;<br style="line-height:25px"><br style="line-height:25px"> /**<br style="line-height:25px"> *ADNListactivityforthePhoneapp.<br style="line-height:25px"> */<br style="line-height:25px"> publicclassADNListextendsListActivity{<br style="line-height:25px"> protectedstaticfinalStringTAG="ADNList";<br style="line-height:25px"> protectedstaticfinalbooleanDBG=false;<br style="line-height:25px"><br style="line-height:25px"> privatestaticfinalString[]COLUMN_NAMES=newString[]{<br style="line-height:25px"> "name",<br style="line-height:25px"> "number"<br style="line-height:25px"> };<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> protectedstaticfinalintNAME_COLUMN=0;<br style="line-height:25px"> protectedstaticfinalintNUMBER_COLUMN=1;<br style="line-height:25px"><br style="line-height:25px"> privatestaticfinalint[]VIEW_NAMES=newint[]{<br style="line-height:25px"> android.R.id.text1,<br style="line-height:25px"> android.R.id.text2<br style="line-height:25px"> };<br style="line-height:25px"><br style="line-height:25px"> protectedstaticfinalintQUERY_TOKEN=0;<br style="line-height:25px"> protectedstaticfinalintINSERT_TOKEN=1;<br style="line-height:25px"> protectedstaticfinalintUPDATE_TOKEN=2;<br style="line-height:25px"> protectedstaticfinalintDELETE_TOKEN=3;<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> protectedQueryHandlermQueryHandler;<br style="line-height:25px"> protectedCursorAdaptermCursorAdapter;<br style="line-height:25px"> protectedCursormCursor=null;<br style="line-height:25px"><br style="line-height:25px"> privateTextViewmEmptyText;<br style="line-height:25px"><br style="line-height:25px"> protectedintmInitialSelection=-1;<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonCreate(Bundleicicle){<br style="line-height:25px"> super.onCreate(icicle);<br style="line-height:25px"> getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);<br style="line-height:25px"> setContentView(R.layout.adn_list);<br style="line-height:25px"> mEmptyText=(TextView)findViewById(android.R.id.empty);<br style="line-height:25px"> mQueryHandler=newQueryHandler(getContentResolver());<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonResume(){<br style="line-height:25px"> super.onResume();<br style="line-height:25px"> query();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonStop(){<br style="line-height:25px"> super.onStop();<br style="line-height:25px"> if(mCursor!=null){<br style="line-height:25px"> mCursor.deactivate();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> protectedUriresolveIntent(){<br style="line-height:25px"> Intentintent=getIntent();<br style="line-height:25px"> if(intent.getData()==null){<br style="line-height:25px"> intent.setData(Uri.parse("content://icc/adn"));<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> returnintent.getData();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidquery(){<br style="line-height:25px"> Uriuri=resolveIntent();<br style="line-height:25px"> if(DBG)log("query:startinganasyncquery");<br style="line-height:25px"> mQueryHandler.startQuery(QUERY_TOKEN,null,uri,COLUMN_NAMES,<br style="line-height:25px"> null,null,null);<br style="line-height:25px"> displayProgress(true);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidreQuery(){<br style="line-height:25px"> query();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidsetAdapter(){<br style="line-height:25px"><br style="line-height:25px"> if(mCursorAdapter==null){<br style="line-height:25px"> mCursorAdapter=newAdapter();<br style="line-height:25px"><br style="line-height:25px"> setListAdapter(mCursorAdapter);<br style="line-height:25px"> }else{<br style="line-height:25px"> mCursorAdapter.changeCursor(mCursor);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> if(mInitialSelection>=0&&mInitialSelection<mCursorAdapter.getCount()){<br style="line-height:25px"> setSelection(mInitialSelection);<br style="line-height:25px"> getListView().setFocusableInTouchMode(true);<br style="line-height:25px"> booleangotfocus=getListView().requestFocus();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> protectedCursorAdapternewAdapter(){<br style="line-height:25px"> returnnewSimpleCursorAdapter(this,<br style="line-height:25px"> android.R.layout.simple_list_item_2,<br style="line-height:25px"> mCursor,COLUMN_NAMES,VIEW_NAMES);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoiddisplayProgress(booleanflag){<br style="line-height:25px"> if(DBG)log("displayProgress:"+flag);<br style="line-height:25px"> mEmptyText.setText(flag?R.string.simContacts_emptyLoading:R.string.simContacts_empty);<br style="line-height:25px"> getWindow().setFeatureInt(<br style="line-height:25px"> Window.FEATURE_INDETERMINATE_PROGRESS,<br style="line-height:25px"> flag?PROGRESS_VISIBILITY_ON:PROGRESS_VISIBILITY_OFF);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privateclassQueryHandlerextendsAsyncQueryHandler{<br style="line-height:25px"> publicQueryHandler(ContentResolvercr){<br style="line-height:25px"> super(cr);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonQueryComplete(inttoken,Objectcookie,Cursorc){//监听QueryComplete事件<br style="line-height:25px"> if(DBG)log("onQueryComplete:cursor.count="+c.getCount());<br style="line-height:25px"> mCursor=c;<br style="line-height:25px"> setAdapter();<br style="line-height:25px"> displayProgress(false);<br style="line-height:25px"> ?<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonInsertComplete(inttoken,Objectcookie,//监听InsertComplete事件<br style="line-height:25px"> Uriuri){<br style="line-height:25px"> if(DBG)log("onInsertComplete:requery");<br style="line-height:25px"> reQuery();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonUpdateComplete(inttoken,Objectcookie,intresult){//监听onUpdateComplete事件<br style="line-height:25px"> if(DBG)log("onUpdateComplete:requery");<br style="line-height:25px"> reQuery();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"> protectedvoidonDeleteComplete(inttoken,Objectcookie,intresult){//监听DeleteComplete事件<br style="line-height:25px"> if(DBG)log("onDeleteComplete:requery");<br style="line-height:25px"> reQuery();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> protectedvoidlog(Stringmsg){<br style="line-height:25px"> Log.d(TAG,"[ADNList]"+msg);<br style="line-height:25px"> }<br style="line-height:25px"> }</wbr>