Android例項剖析筆記(一)

2021-09-30 08:48:44 字數 2897 閱讀 6589

phinecos(洞庭散人) 專注於開源技術的研究與應用 android例項剖析筆記(一) 開卷語 俗話說,「熟讀唐詩三百首,不會作詩也會吟」。最近收集了很多android的示例**,從這些**的閱讀和實驗中學習到很多知識,從而產生寫這個系列的打算,目標就是一步步跟著例項進行動手實作,真正從「做」中體會和學習android開發。 本文是這個系列的第一篇,目標是android自帶的乙個範例程式:記事本,將分為四篇文章進行詳細介紹。 預備知識 搭建開發環境,嘗試編寫」hello world」,了解android的基本概念,熟悉android的api(官方文件中都有,不贅述)。 程式截圖 先來簡單了解下程式執行的效果 程式入口點 類似於win32程式裡的winmain函式,android自然也有它的程式入口點。它通過在androidmanifest.xml檔案中配置來指明,可以看到名為noteslist的activity節點下有這樣乙個intent-filter,其action為android.intent.action.main, category指定為 android.intent.category.launcher,這就指明了這個activity是作為入口activity,系統查詢到它後,就會建立這個activity例項來執行,若未發現就不啟動(你可以把main改名字試試)。

noteslist詳解 就從入口點所在的activity(見圖1)開始,可以看到這個activity最重要的功能就是顯示日誌列表。這個程式的日誌都存放在sqlite資料庫中,因此需要讀取出所有的日誌記錄並顯示。 先來看兩個重要的私有資料,第乙個projection欄位指明了「日誌列表「所關注的資料庫中的字段(即只需要id和title就可以了)。 private static final string projection = new string ; 第二個欄位column_index_title指明title欄位在資料表中的索引。 private static final int column_index_title = 1; 然後就進入第乙個呼叫的函式oncreate。 intent intent = getintent(); if (intent.getdata() == null) 因為noteslist這個activity是系統呼叫的,此時的intent是不帶資料和操作型別的,系統只是在其中指明了目標元件是notelist,所以這裡把」content:// com.google.provider.notepad/notes」儲存到intent裡面,這個uri位址指明了資料庫中的資料表名(參見以後的notepadprovider類),也就是儲存日誌的資料表notes。 cursor cursor = managedquery(getintent().getdata(), projection, null, null, notes.default_sort_order); 然後呼叫managedquery函式查詢出所有的日誌資訊,這裡第乙個引數就是上面設定的」 content:// com.google.provider.notepad/notes」這個uri,即notes資料表。projection 字段指明了結果中所需要的字段,notes.default_sort_order 指明了結果的排序規則。實際上managedquery並沒有直接去查詢資料庫,而是通過content provider來完成實際的資料庫操作,這樣就實現了邏輯層和資料庫層的分離。 ******cursoradapter adapter = new ******cursoradapter(this, r.layout.noteslist_item, cursor, new string , new int ); setlistadapter(adapter); 查詢出日誌列表後,構造乙個cursoradapter,並將其作為list view的資料來源,從而在介面上顯示出日誌列表。可以看到,第二個引數是r.layout.noteslist_item,開啟對應的noteslist_item.xml檔案,

發現沒有?它也有com.google.provider.notepad,這個是content:的一部分,同時

上面第乙個intent-filter中有乙個action 名為android.intent.action.edit,而前面我們建立的intent也正好是 intent.action_edit=」 android.intent.action.edit」,想必大家已經明白是怎麼回事了吧。 下面就進入activity選擇機制了: 系統從intent中獲取道uri,得到了content:,去掉開始的content:標識,得到com.google.provider.notepad/notes/1,然後獲取前面的com.google.provider.notepad,然後就到androidmanfest.xml中找到authorities為com.google.provider.notepad的provider,這個就是後面要講的contentprovider,然後就載入這個content provider。

在這裡是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) } 上面的surimatcher.match是用來檢測uri是否能夠被處理,而surimatcher.match(uri)返回值其實是由 surimatcher = new urimatcher(urimatcher.no_match); surimatcher.adduri(notepad.authority, "notes", notes); surimatcher.adduri(notepad.authority, "notes/#", note_id); 決定的。 然後系統使用獲得的" vnd.android.cursor.item/vnd.google.note "和」android.intent.action.edit」到androidmanfest.xml中去找匹配的activity.

Android網路請求框架剖析一 Okhttp

封裝後的使用方式請直接跳轉上述鏈結。首先引入 as兩種引入方法說了很多遍了其實效果一樣 在gradle裡dependencies新增 然後sync或者rebuild一下 然後使用 get 獲取乙個網頁的內容。建立okhttpclient物件 建立乙個request request.builder設定...

正規化應用例項剖析

下面以乙個學校的學生系統為例分析說明,這幾個正規化的應用。首先第一正規化 1nf 資料庫表中的字段都是單一屬性的,不可再分。這個單一屬性由基本型別 構成,包括整型 實數 字元型 邏輯型 日期型等。在當前的任何關聯式資料庫管理系統 dbms 中,傻瓜也不可能做出不符合第一正規化的資料庫,因為這些 db...

python原始碼剖析筆記(一)

define pyobject head pyobject head extra py ssize t ob refcnt struct typeobject ob type define pyobject var head pyobject head py ssize t ob size numb...