




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、開卷語 俗話說,“熟讀唐詩三百首,不會作詩也會吟”。最近收集了很多Android的示例代碼,從這些代碼的閱讀和實驗中學習到很多知識,從而產生寫這個系列的打算,目標就是一步步跟著實例進行動手實作,真正從“做”中體會和學習Android開發。本文是這個系列的第一篇,目標是Android自帶的一個范例程序:記事本,將分為四篇文章進行詳細介紹。預備知識 搭建開發環境,嘗試編寫”Hello World”,了解Android的基本概念,熟悉Android的API(官方文檔中都
2、有,不贅述)。程序截圖 先來簡單了解下程序運行的效果程序入口點 類似于win32程序里的WinMain函數,Android自然也有它的程序入口點。它通過在AndroidManifest.xml文件中配置來指明,可以看到名為NotesList的activity節點下有這樣一個intent-filter,其action為ent.action.MAIN, Category指定為 ent.category.LAUNCHER,這就指明了這個activity是作為入口activity,系統查找
3、到它后,就會創建這個activity實例來運行,若未發現就不啟動(你可以把MAIN改名字試試)。 <intent-filter> <action android:name="ent.action.MAIN" />
4、60; <category android:name="ent.category.LAUNCHER" /> </intent-filter>NotesList詳解 就從入口點所在的activity(見圖1)開始,可以
5、看到這個activity最重要的功能就是顯示日志列表。這個程序的日志都存放在Sqlite數據庫中,因此需要讀取出所有的日志記錄并顯示。先來看兩個重要的私有數據,第一個PROJECTION字段指明了“日志列表“所關注的數據庫中的字段(即只需要ID和Title就可以了)。 private static final String PROJECTION = new String
6、160; Notes._ID, / 0 Notes.TITLE, / 1 第二個字段COLUMN_INDEX_TITLE指明title字段在數據表中的索引。private static final int COLUMN_INDEX_TITLE =
7、160;1;然后就進入第一個調用的函數onCreate。 Intent intent = getIntent(); if (intent.getData() = null)
8、 intent.setData(Notes.CONTENT_URI); 因為NotesList這個activity是系統調用的,此時的intent是不帶數據和操作類型的,系統只是在其中指明了目標組件是Notelist,所以這里把”content:/ vider.NotePad/notes”保存到intent里面,這個URI地址指明了數
9、據庫中的數據表名(參見以后的NotePadProvider類),也就是保存日志的數據表notes。 Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, Notes.DEFAULT_SORT_ORDER); 然后調用managedQuery函數查詢出所有的日志信息,這
10、里第一個參數就是上面設置的” content:/ vider.NotePad/notes”這個URI,即notes數據表。PROJECTION 字段指明了結果中所需要的字段,Notes.DEFAULT_SORT_ORDER 指明了結果的排序規則。實際上managedQuery并沒有直接去查詢數據庫,而是通過Content Provider來完成實際的數據庫操作,這樣就實現了邏輯層和數據庫層的分離。 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,&
11、#160;R.layout.noteslist_item, cursor, new String Notes.TITLE , new int android.R.id.text1 ); setListAdapte
12、r(adapter); 查詢出日志列表后,構造一個CursorAdapter,并將其作為List View的數據源,從而在界面上顯示出日志列表。可以看到,第二個參數是,打開對應的noteslist_item.xml文件,<TextView xmlns:android=" android:id="android:id/text1" android:layout_width="fil
13、l_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:
14、paddingLeft="5dip" android:singleLine="true"/>就是用來顯示一條日志記錄的TextView,最后兩個字段指明了實際的字段映射關系,通過這個TextView來顯示一條日志記錄的title字段。處理“選擇日志”事件 既然有了“日志列表”,就自然要考慮如何處理某一條日志的單擊事件,這通過重載onListItemClick方法來完成, Override
15、 protected void onListItemClick(ListView l, View v, int position, long id) Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
16、160; String action = getIntent().getAction(); if (Intent.ACTION_PICK.equals(action) | Intent.ACTION_GET_CONTENT.equals(action)
17、160; / The caller is waiting for us to return a note selected by / the user. The have clicked
18、0;on one, so return it now. setResult(RESULT_OK, new Intent().setData(uri); else &
19、#160; / Launch activity to view/edit the currently selected item startActivity(new Intent(Intent.ACTION_EDIT, uri);
20、; 首先通過”content:/ vider.NotePad/notes”和日志的id 號拼接得到選中日志的真正URI,然后創建一個新的Intent,其操作類型為Intent.ACTION_EDIT,數據域指出待編輯的日志URI(這里只分析else塊)。Intent深度剖析那么,上面這句startActivity(new Intent(Intent.ACTION_EDIT, uri)執行后會發生什么事情呢?這時候Android系統就跳出來接管了,它會根據intent中的信息找到對應的
21、activity,在這里找到的是NoteEditor這個activity,然后創建這個activity的實例并運行。那么,Android又是如何找到NoteEditor這個對應的activity的呢?這就是intent發揮作用的時刻了。new Intent(Intent.ACTION_EDIT, uri)這里的Intent.ACTION_EDIT=” ent.action.EDIT”,另外通過設置斷點,我們看下這里的uri值: 可以看到選中的日志條目的URI是:。然后我們再來看下An
22、droidmanfest.xml,其中有這個provider<provider android:name="NotePadProvider" android:authorities="vider.NotePad" />
23、0; 發現沒有?它也有,這個是的一部分,同時 <activity android:name="NoteEditor" android:theme="android:style/Theme.Light" a
24、ndroid:label="string/title_note" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation"
25、0; > <!- This filter says that we can view or edit the data of
26、 a single note -> <intent-filter android:label="string/resolve_edit">
27、160; <action android:name="ent.action.VIEW" /> <action android:name="ent.action.EDIT" />
28、160; <action android:name="com.android.notepad.action.EDIT_NOTE" /> <category android:name="ent.catego
29、ry.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-
30、filter> <!- This filter says that we can create a new note inside o
31、f a directory of notes. -> <intent-filter> <action android:name="ent.action.
32、INSERT" /> <category android:name="ent.category.DEFAULT" />
33、<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> </activity>上面第一個intent-filter中有一個action 名為,而前面我們創建的Intent也正好
34、是Intent.ACTION_EDIT=” ent.action.EDIT”,想必大家已經明白是怎么回事了吧。下面就進入activity選擇機制了:系統從intent中獲取道uri,得到了content:/vider.NotePad/notes/1,去掉開始的content:標識,得到vider.NotePad/notes/1,然后獲取前面的,然后就到Androidmanfest.xml中找到authorities為的provider,這個就是后面要講的contentprovider,然后就加載這個content p
35、rovider。 <provider android:name="NotePadProvider" android:authorities="vider.NotePad" />在這
36、里是NotePadProvider,然后調用NotePadProvider的gettype函數,并把上述URI傳給這個函數,函數返回URI所對應的類型(這里返回Notes.CONTENT_ITEM_TYPE,代表一條日志記錄,而CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note ")。 Override public String getType(Uri uri)
37、0; switch (sUriMatcher.match(uri) case NOTES: return Notes.CONTENT_TYPE; case
38、160;NOTE_ID: return Notes.CONTENT_ITEM_TYPE; default: throw new IllegalArgumentException("U
39、nknown URI " + uri); 上面的sUriMatcher.match是用來檢測uri是否能夠被處理,而sUriMatcher.match(uri)返回值其實是由決定的。 sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH)
40、; sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES); sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);然后系統使用獲得的" vnd.android.cursor.item/vnd.google.note "和”ent.action.EDIT”到androidmanfest.xml中去找匹配的activity. <intent-filter android:label="string/resolve_edit"&g
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 新車趣味測試題及答案
- 母豬護理中的醫療保健措施分析試題及答案
- 聯通通信安全試題及答案
- 高級配煤工試題及答案
- 藥物治療中現代技術的應用與影響試題及答案
- 職工競聘面試題及答案
- 車體安全測試題及答案
- 闖關成功2025年初級會計師考試試題及答案
- 寵物考試試題及答案
- 文化產業管理政策與實踐試題及答案
- AGC 系統安全事故應急處置程序
- 變位齒輪與變位齒輪傳動
- 二級精神病醫院評價細則
- TGIA 004-2020 垃圾填埋場地下水污染防治技術指南
- GB/T 148-1997印刷、書寫和繪圖紙幅面尺寸
- 《思想道德與法治》 課件 第三章 弘揚中國精神
- 人教版小學數學四年級下冊平均數教學教材課件
- (更新版)中國移動政企行業認證題庫大全-上(單選題匯總-共3部分-1)
- 中國古錢幣課件5(宋元明清)
- 2022年小升初入學考試數學真題重慶市巴川中學初一新生入學水平測試
- 品質控制計劃(QC工程圖)
評論
0/150
提交評論