![十年磨一劍筆記android基礎(chǔ)day10內(nèi)容提供者_第1頁](http://file4.renrendoc.com/view/a8bf5d8733a4286ae378c0b294aec302/a8bf5d8733a4286ae378c0b294aec3021.gif)
![十年磨一劍筆記android基礎(chǔ)day10內(nèi)容提供者_第2頁](http://file4.renrendoc.com/view/a8bf5d8733a4286ae378c0b294aec302/a8bf5d8733a4286ae378c0b294aec3022.gif)
![十年磨一劍筆記android基礎(chǔ)day10內(nèi)容提供者_第3頁](http://file4.renrendoc.com/view/a8bf5d8733a4286ae378c0b294aec302/a8bf5d8733a4286ae378c0b294aec3023.gif)
![十年磨一劍筆記android基礎(chǔ)day10內(nèi)容提供者_第4頁](http://file4.renrendoc.com/view/a8bf5d8733a4286ae378c0b294aec302/a8bf5d8733a4286ae378c0b294aec3024.gif)
![十年磨一劍筆記android基礎(chǔ)day10內(nèi)容提供者_第5頁](http://file4.renrendoc.com/view/a8bf5d8733a4286ae378c0b294aec302/a8bf5d8733a4286ae378c0b294aec3025.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
報名資源總連接 (了解)為什么需要內(nèi)容提供(掌握)內(nèi)容提供者工作原class extends重寫方法}<provider1找到要的uri(掌握)自己實現(xiàn)一個內(nèi)容提供者的增刪改 表 Student A:應(yīng)用要對B應(yīng)用里面的數(shù)據(jù)庫 表和student執(zhí)行crud的操作B應(yīng)用使用ContentResolver來進行操如果有的人測試使用的是 4.4版本注意publicclassMyContentProviderextendsContentProviderprivateOpenHelperhelper;//數(shù)據(jù)庫的幫助類privatefinalstaticStringauthority= privatefinalstatic S=privatefinalstaticint_ID=10;privatefinalstaticintSTUDENTS=2;privatestaticUriMatchermatcher=newUriMatcher(UriMatcher.NO_MATCH);//authority path組成了 code該uri的唯一標matcher.addURI(authority," ",S);//content://authority/matcher.addURI(authority," /#",_ID);//表里面指定id的記錄}publicbooleanonCreate()helper=MyOpenHelper.getInstance(getContext());returnfalse;}publicCursorquery(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder){Databasedb=helper.getReadableDatabase();Cursorret=null;intcode=matcher.match(uri);switch(code){caseret=db.query("",projection,selection,selectionArgs,null,null,sortOrder);caseret=db.query("student",projection,selection,selectionArgs,null,null,sortOrder);thrownewIllegalArgumentException("沒有這樣的}return}publicUriinsert(Uriuri,ContentValuesvalues){Databasedb=helper.getWritableDatabase();intcode=matcher.match(uri);switch(code){caseS:db.insert("",null,caseSTUDENTS:db.insert("student",null,values);thrownewIllegalArgumentException("沒有這樣的}return}publicintdelete(Uriuri,Stringselection,String[]selectionArgs){Databasedb=helper.getWritableDatabase();intcode=switch(code){caseS:db.delete("",selection,casedb.delete("student",selection,selectionArgs);thrownewIllegalArgumentException("沒有這樣的}return}publicintupdate(Uriuri,ContentValuesvalues,Stringselection,String[]selectionArgs){Databasedb=helper.getWritableDatabase();intcode=matcher.match(uri);switch(code){caseS:db.update("",values,selection,casedb.update("student",values,selection,selectionArgs);thrownewIllegalArgumentException("沒有這樣的}return} //vnd.android.cursor.dir/publicStringgetType(Uriuri){Stringtype=null;intcode=matcher.match(uri);switch(code){casetype="vnd.android.cursor.item/";casetype="vnd.android.cursor.dir/";casetype="vnd.android.cursor.dir/student";}return}}內(nèi)容提供者常用的apiUricontent://authortyDatabaseOpenHelperContentValues 獲取10 (掌握)利用內(nèi)容提供者聯(lián)系沒有數(shù)據(jù)庫數(shù)據(jù)是從其他的應(yīng)用程序里面內(nèi)容提供者的數(shù)據(jù):聯(lián)系人的表聯(lián)系人的唯一標示_id:數(shù)據(jù)表的是聯(lián)系的數(shù)號碼 郵箱。。raw_contact_id:iddata2:一定要確一定要確 的操作 datapublicvoidget(ViewList<Info>infos=newArrayList< com.android.contactsContentResolvercr=Uriuri=Uri.parse(" Cursorcursor=cr.query(uri,newString[]{"_id"},null,null,null);int_id=Infoinfo=newInfo();info._id=_id;//datadatauri=Uri.parse(" Cursorcursor2=cr.query(uri,newString[]{"data1","mimetype"},null,null,null);/*String[]names=cursor2.getColumnNames();for(Stringstring:names){Stringdata1=cursor2.getString(0);Stringmimetype=cursor2.getString(1);info.phone=}elseif("vnd.android.cursor.item/name".equals(mimetype)){//=data1; =}}}for(Infoinfo:infos){}}(掌握)利用內(nèi)容提供者插系publicvoidinsert(View//插系 //1先插入一條記錄到raw_contacts //2data表里面插入數(shù)據(jù)還要帶上 ContentResolvercr=getContentResolver();Uriuri=Uri.parse(" ContentValuesvalues=newContentValues();Uriinsert=cr.insert(uri,=uri= //插入values.put("raw_contact_idraw_contact_id);values.put("data1","親幾圈");values.put("mimetype","vnd.android.cursor.item/name");cr.insert(uri,values);values.put("raw_contact_id",raw_contact_id);values.put("data1"," values.put("mimetype","vnd.android.cursor.item/phone_v2");values.put("data2",1);values.put("raw_contact_id",raw_contact_id);values.put("data1","qi values.put("mimetype","vnd.android.cursor.item/_v2");values.put("data2",1);}內(nèi)容提供者在開發(fā)中自己寫不多。的是內(nèi)容提供者 確定要的應(yīng)看文件里面的provider節(jié) :1類23是否需看類:Ctrl+ 輸入找到需要使用的Uri(一定要看該應(yīng)用程序里面數(shù)據(jù)庫的表(掌握)內(nèi)容觀察者的原L應(yīng)用publicclassMainActivityextendsActivityprivatestaticfinalStringTAG="MainActivity";protectedvoidonCreate(BundlesavedInstanceState){ContentResolvercr=Uriuri=Uri.parse(" cr.query(uri,null,null,null,null);,"MyContentObserverobserver=newMyContentObserver(new//使用內(nèi)容觀察者uri數(shù)據(jù)的改 cr.registerContentObserver(uri,true,observer);}privateclassMyContentObserverextendspublicMyContentObserver(Handlerhandler){}//uri數(shù)據(jù)的改變發(fā)出改變通知publicvoidonChange(booleanselfChangeLog.i(TAG,"數(shù)據(jù)已經(jīng)發(fā)生改變");}}}(掌握)利用內(nèi)容觀察者用戶數(shù)據(jù)1數(shù)據(jù)在哪里:數(shù)據(jù)的2該數(shù)據(jù)庫里面的數(shù)據(jù)是通過內(nèi)容提供者對外3的內(nèi)容提供者已經(jīng)發(fā)出改變通4使用內(nèi)容觀察者來檢測的變publicclassMainActivityextendsActivitypublicstaticfinalStringTAG="MainActivity";privateContentResolvercr;privateMyContentObserverprotectedvoidonCreate(BundlesavedInstanceState){//檢測的變cr=Uriuri=observer=newMyContentObserver(newHandler());cr.registerContentObserver(uri,true,observer);}privateclassMyContentObserverextendspublicMyContentObserver(Handlerhandler){}publicvoidonChange(booleanselfChange){//查詢出插入的那條ContentResolvercrgetContentResolver();Uriuri=Uri.parse("content://sms");Cursorcursorcr.query(urinewString[]{"body"},nullnull"_iddesc");Stringbody=cursor.getString(0);Log.i(TAG,"body:"+body);}}protectedvoidonDestroy(){}}(掌握4.0一下 publicclassMainActivityextendsActivityprivateMyAdapterprotectedvoidonCreate(BundlesavedInstanceState){ListViewlv=(ListView)List<CallInfo>infos=CallInfoService.getCallInfos(this);adapter=newMyAdapter(infos);publicbooleanonItemLongClick(AdapterView<?>parent,Viewview,intposition,longid){CallInfoinfo=(CallInfo)adapter.getItem(position);finalStringnumber=infonumber;String[]items=newString[]{"號碼到撥號盤","撥號","newpublicvoidonClick(DialogInterfacedialog,intwhich){switch(which){case//號碼到撥號case1://撥號權(quán)case2:}}returnfalse;}}privateclassMyAdapterextendsprivateList<CallInfo>infos;privateLayoutInflatermInflater;publicMyAdapter(List<CallInfo>infos){s=mInflater=(LayoutInflater)}publicintgetCount(){returninfos.size();}publicObjectgetItem(intposition)//TODOAuto-generatedmethodstubreturninfos.get(position);}publiclonggetItemId(intposi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 泰州2025年江蘇泰州職業(yè)技術(shù)學(xué)院口腔門診部招聘合同制口腔助理醫(yī)師筆試歷年參考題庫附帶答案詳解
- 2025至2031年中國塑料食槽行業(yè)投資前景及策略咨詢研究報告
- 2025至2030年中國鋼木木中央實驗臺數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國噴絲板數(shù)據(jù)監(jiān)測研究報告
- 摩托車博物館與摩托車文化傳承考核試卷
- 二零二五年度外墻保溫施工進度與付款管理合同
- 化妝品產(chǎn)品知識及使用方法考核試卷
- 房地產(chǎn)信托投資分析考核試卷
- 2025-2030年口腔手術(shù)麻醉系統(tǒng)企業(yè)制定與實施新質(zhì)生產(chǎn)力戰(zhàn)略研究報告
- 2025-2030年振動傳感器故障診斷系統(tǒng)行業(yè)跨境出海戰(zhàn)略研究報告
- 礦井通風(fēng)安全培訓(xùn)課件
- 2024年中國國際投資促進中心限責(zé)任公司招聘高頻考題難、易錯點模擬試題(共500題)附帶答案詳解
- 苯胺合成靛紅工藝
- 質(zhì)量保證發(fā)展史和國外相關(guān)標準簡介
- 三年級上冊數(shù)學(xué)脫式計算大全600題及答案
- 計算機控制系統(tǒng) 課件 第10章 網(wǎng)絡(luò)化控制系統(tǒng)的分析與設(shè)計
- 魯教版(五四制)七年級數(shù)學(xué)上冊期末考試卷-附帶答案
- 南京大學(xué)儀器分析習(xí)題集
- 空調(diào)維保應(yīng)急預(yù)案
- 小學(xué)六年級數(shù)學(xué)上冊解決問題專項必考題西師大版
- 2023年高考語文全國乙卷作文范文及導(dǎo)寫(解讀+素材+范文)課件版
評論
0/150
提交評論