編輯:關於 Android 程式設計
ContentProvider 是 Android 四大組件之一,專門用來解決跨應用共享數據的問題。
換句話說,ContentProvider 站在數據與外部調用者之間充當中介:數據本身可以存在 SQLite、文件甚至內存裡,外部則永遠只跟 ContentResolver 打交道,通過 URI 定位資源。
需要一個子類,至少把 query、insert、update、delete 這四個方法實現出來:
public boolean onCreate():ContentProvider 創建後調用。其他應用第一次訪問時,該實例才會被創建。public Uri insert(Uri uri, ContentValues values):按 URI 定位,插入 values 所攜帶的數據。public int delete(Uri uri, String selection, String[] selectionArgs):按 URI 刪除 selection 條件匹配的全部記錄。public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs):按 URI 更新 selection 條件匹配的全部記錄。public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder):按 URI 查詢 selection 條件匹配的全部記錄。public String getType(Uri uri):返回當前 URI 所代表的 MIME 類型。getType() 的返回值有約定俗成的寫法:
vnd.android.cursor.dir/ 開頭。vnd.android.cursor.item/ 開頭。這個約定並非可有可無的形式主義,ContentResolver 會依賴它判斷返回的 Cursor 是列表還是單項。
最後別忘了聲明這個組件,關鍵是指定 android:authorities 屬性:
<provider
android:name=".MyContentProvider"
android:authorities="com.example.myprovider" />
authorities 是 ContentProvider 在系統裡的唯一標識,外部應用正是通過 content://com.example.myprovider/... 這樣的 URI 找到它,因此命名習慣上一般直接用包名。
Android 圖形變換特效详解:Matrix 矩陣原理與實作(四)
今天我們來談談 Android 圖片變形的特效。上一講我們談的是透過 ColorMatrix 類實現的色彩特效,主要透過色彩矩陣的係數與每個像素點對應的顏色偏移量來改變顏
Android 用 ExpandableListView 實作三層樹狀選單
在 Android 上要實作樹狀選單,通常會想到 ExpandableListView(也有高手自行繼承 ListView 或 LinearLayout 來做),但它一般
ScrollView 嵌套 ListView 的五種解法與優缺點全面比較
實務上很常遇到 ScrollView 裡塞 ListView 的需求,網路上的解法五花八門卻往往講不清楚。我陸續試過多種做法,各有取捨,以下從「為什麼要這樣做」、「會出什
Volley 網絡請求框架深度解析:Android 學習之路
為什麼我們需要 Volley 在 Android 上做網絡請求,早期基本只有兩條路:HttpURLConnection 與 HttpClient。問題不在於它們不能用,而