安卓開發(fā)實(shí)例一_第1頁
安卓開發(fā)實(shí)例一_第2頁
安卓開發(fā)實(shí)例一_第3頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、開卷語俗話說, “熟讀唐詩三百首,不會作詩也會吟 ”。最近收集了很多 Android 的示例代碼,從這些代碼的閱讀和實(shí)驗(yàn)中學(xué)習(xí)到很多知識, 從而產(chǎn)生寫這個系列的打算,目標(biāo)就是一步步跟著實(shí)例進(jìn)行動手實(shí)作,真正從 “做”中體會和學(xué)習(xí)Android開發(fā)。本文是這個系列的第一篇,目標(biāo)是 Android 自帶的一個例程序:記事本,將分為四篇文章進(jìn)行詳細(xì)介紹。預(yù)備知識搭建開發(fā)環(huán)境,嘗試編寫” Hello World,”了解 Android的基本概念,熟悉 Android 的 API( 官方文檔中都有,不贅述 ) 。程序截圖先來簡單了解下程序運(yùn)行的效果程序入口點(diǎn)類似于 win32程序里的 WinMain函數(shù)

2、, Android自然也有它的程序入口點(diǎn)。它通過在 AndroidManifest.xml文件中配置來指明,可以看到名為NotesList的 activity節(jié)點(diǎn)下有這樣一個intent-filter,其 action為ent.action.MAIN, Category指定為ent.category.LAUNCHER,這就指明了這個 activity是作為入口activity,系統(tǒng)查找到它后, 就會創(chuàng)建這個activity實(shí)例來運(yùn)行,若未發(fā)現(xiàn)就不啟動 ( 你可以把 MAIN 改名字試試 ) 。<intent-filter><acti

3、onandroid:name ="ent.action.MAIN"/><categoryandroid:name ="ent.category.LAUNCHER"/></ intent-filter>NotesList詳解就從入口點(diǎn)所在的activity(見圖 1) 開始,可以看到這個 activity最重要的功能就是顯示日志列表。這個程序的日志都存放在Sqlite數(shù)據(jù)庫中,因此需要讀取出所有的日志記錄并顯示。先來看兩個重要的私有數(shù)據(jù),第一個PROJECTION 字段指明了 “日志

4、列表 “所關(guān)注的數(shù)據(jù)庫中的字段(即只需要和 Title 就可以了)。IDprivatestaticfinalStringPROJECTION=newStringNotes._ID,/0Notes.TITLE,/1;第二個字段COLUMN_INDEX_TITLE指明title字段在數(shù)據(jù)表中的索引。privatestaticfinalintCOLUMN_INDEX_TITLE=然后就進(jìn)入第一個調(diào)用的函數(shù)onCreate。Intentintent=getIntent();if(intent.getData()=null )1;intent.setData(Notes.CONTENT_URI);因?yàn)?

5、NotesList這個 activity是系統(tǒng)調(diào)用的, 此時的 intent是不帶數(shù)據(jù)和操作類型的,系統(tǒng)只是在其中指明了目標(biāo)組件是Notelist,所以這里把 ”content:/ .vider.NotePad/notes”保存到 intent里面,這個URI 地址指明了數(shù)據(jù)庫中的數(shù)據(jù)表名(參見以后的NotePadProvider類),也就是保存日志的數(shù)據(jù)表notes 。,CursorPROJECTION, null ,cursor null ,=managedQuery(getIntent().getData()Notes.DEFAULT_SORT_ORDER);然后調(diào)用

6、 managedQuery函數(shù)查詢出所有的日志信息,這里第一個參數(shù)就是上面設(shè)置的” content:/ .vider.NotePad/notes”這個 URI ,即 notes 數(shù)據(jù)表。 PROJECTION 字段指明了結(jié)果中所需要的字段,Notes.DEFAULT_SORT_ORDER指明了結(jié)果的排序規(guī)則。實(shí)際上managedQuery 并沒有直接去查詢數(shù)據(jù)庫, 而是通過 Content Provider 來完成實(shí)際的數(shù)據(jù)庫操作,這樣就實(shí)現(xiàn)了邏輯層和數(shù)據(jù)庫層的分離。SimpleCursorAdapteradapter= new SimpleCursorAdapter( t

7、his ,R.layout.noteslist_item,cursor,new String Notes.TITLE ,new int android.R.id.text1);setListAdapter(adapter);查詢出日志列表后,構(gòu)造一個CursorAdapter,并將其作為 List View的數(shù)據(jù)源,從而在界面上顯示出日志列表??梢钥吹?,第二個參數(shù)是R.layout.noteslist_item,打開對應(yīng)的 noteslist_item.xml文件,<TextViewxmlns:android ="schemas.android./apk/res/android

8、"android:id="android:id/text1"android:layout_width="fill_parent"android:layout_height="?android:attr/listPreferredItemHeight"android:textAppearance="?android:attr/textAppearanceLarge"android:gravity="center_vertical"android:paddingLeft="5di

9、p"android:singleLine="true"/>就是用來顯示一條日志記錄的 TextView, 最后兩個字段指明了實(shí)際的字段映射關(guān)系,通過這個 TextView 來顯示一條日志記錄的 title 字段。處理 “選擇日志 ”事件既然有了 “日志列表 ”,就自然要考慮如何處理某一條日志的單擊事件,這通過重載 onListItemClick 方法來完成,Overrideprotectedposition,longUrivoidid)urionListItemClick(ListViewl,Viewv,=ContentUris.withAppendedId

10、(getIntent().gintetData(),id);Stringaction=getIntent().getAction();if(Intent.ACTION_PICK.equals(action)|Intent.ACTION_GET_CONTENT.equals(action)/Thecalleriswaitingforturnanoteselectedby/theuser.Thehaveclickedustoreonone,soreturnitnow.setResult(RESULT_OK,newIntent().setData(uri);else/Launchactivityto

11、view/editthecurrentlyselecteditemstartActivity(newIntent(Intent.ACTION_EDIT,uri);首先通過 ”content:/ .vider.NotePad/notes”和日志的 id 號拼接得到選中日志的真正 URI, 然后創(chuàng)建一個新的 Intent, 其操作類型為Intent.ACTION_EDIT,數(shù)據(jù)域指出待編輯的日志 URI(這里只分析 else 塊)。Intent深度剖析那么,上面這句 startActivity(newIntent(Intent.ACTION_EDIT, uri)執(zhí)行后會發(fā)生什么

12、事情呢?這時候Android系統(tǒng)就跳出來接管了,它會根據(jù)intent中的信息找到對應(yīng)的activity,在這里找到的是NoteEditor這個activity,然后創(chuàng)建這個activity的實(shí)例并運(yùn)行。是那么,Android又是如何找到intent發(fā)揮作用的時刻了。NoteEditor這個對應(yīng)的activity的呢?這就newIntent(Intent.ACTION_EDIT,uri)這里的 Intent. ACTION_EDIT 設(shè)置斷點(diǎn),我們看下這里的 uri=” ent.action.ED 值:IT ”,另外通過可以看到選中的日志條目的URI 是:content:/

13、.vider.NotePad/notes/1。然后我們再來看下Androidmanfest.xml,其中有這個 provider<providerandroid:name ="NotePadProvider"android:authorities=".vider.NotePad"/>發(fā)現(xiàn)沒有?它也有 vider.NotePad,這個是content:/.vider.NotePad/notes/1的一部分,同時<activityandroid:name

14、 ="NoteEditor"android:theme="android:style/Theme.Light"android:label="string/title_note"android:screenOrientation="sensor"tion"android:configChanges ="keyboardHidden|orienta><!- Thisfiltersaysthat we can viewor editthedata ofasinglenote-><

15、;intent-filterandroid:label ="string/resolve_edit"><actionandroid:name ="ent.action.VIEW"/><actionandroid:name ="ent.action.EDIT"/>ad.action.EDIT_NOTE"/><actionandroid:name =".android.notep<categoryandroid:name =&q

16、uot;ent.category.DEFAULT"/><dataandroid:mimeType ="vnd.android.cursor.item/vnd.google.note"/></ intent-filter><!-This filtersaysthatwe cancreate a newnoteinsideofadirectoryofnotes.-><intent-filter><actionandroid:name ="ent.actio

17、n.INSERT"/><categoryandroid:name ="ent.category.DEFAULT"/><dataandroid:mimeType ="vnd.android.cursor.dir/vnd.google.note"/></ intent-filter></ activity>上面第一個 intent-filter中有一個 action名為ent.action.EDIT,而前面我們創(chuàng)建的 Intent也正好是Intent.

18、ACTION_EDIT=” ent.action.EDIT,想”必大家已經(jīng)明白是怎么回事了吧。下面就進(jìn)入 activity選擇機(jī)制了:系統(tǒng)從intent中獲取道uri,得到了content:/.vider.NotePad/notes/1, 得到 vider.NotePad/notes/1,去掉開始的 content:然后獲取前面的標(biāo)識,vider.NotePad,然后就到 Androidmanfest.xml中找到authorities為 vider.NotePad的

19、provider,這個就是后面要講的 contentprovider,然后就加載這個content provider。<providerandroid:name ="NotePadProvider"android:authorities=".vider.NotePad"/>在這里是 NotePadProvider, 然后調(diào)用 NotePadProvider 的 gettype 函數(shù),并把上述 URI 傳給這個函數(shù),函數(shù)返回 URI 所對應(yīng)的類型(這里返回Notes.CONTENT_ITEM_TYPE ,代表一條日志記錄,而

20、 CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note" )。OverridepublicString switch case casegetType(Uriuri)(sUriMatcher.match(uri)NOTES:returnNotes.CONTENT_TYPE;NOTE_ID:returnNotes.CONTENT_ITEM_TYPE;default :thrownewIllegalArgumentException("UnknownURI"+uri);上面的 sUriMa

21、tcher.match 是用來檢測 uri 是否能夠被處理,而 sUriMatcher .match(uri) 返回值其實(shí)是由決定的。sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES);sUriMatcher.addURI(NotePad.AUTHORITY,"notes/#",NOTE_ID);然后系統(tǒng)使用獲得的 " vnd.android.cursor.item/vnd.google.note "和 ”ent.action.EDIT”到 androidmanfest.xml中去找匹配的activity.<intent-filterandroid:label ="string/resolve_edit"><actionandroid:name ="ent.acti

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論