Android 應用間數據共享的標準機制是 ContentProvider。本文說明如何訪問系統備忘錄或第三方應用的數據,以及遇到權限問題時的解決思路。
<provider>,指定 authorities、權限、導出屬性ContentResolver 配合 URI 進行 CRUD 操作com.android.providers.calendar、com.google.android.keep 等)<uses-permission android:name="android.permission.READ_CALENDAR" />Uri uri = Uri.parse("content://com.android.calendar/events");
String[] projection = {"_id", "title", "dtstart", "dtend"};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndex("title"));
long start = cursor.getLong(cursor.getColumnIndex("dtstart"));
// 處理數據
}
cursor.close();
}
android:exported="true" 的 provider| 問題 | 原因 | 解決 |
|---|---|---|
| SecurityException | 缺少權限或簽名不一致 | 補全 Manifest 權限,確認簽名 |
| 查詢返回 null | URI 錯誤、Provider 未導出、數據為空 | 驗證 URI、聯繫提供方 |
| Android 10+ 存儲限制 | 分區存儲、Scoped Storage | 使用 MediaStore、Storage Access Framework |
CursorLoader(配合 LoaderManager)自動管理生命周期Android 跨應用數據訪問完全依賴 ContentProvider。系統應用查官方文檔,第三方應用問開發者。無 Provider 則無標準訪問途徑,切勿嘗試繞過沙箱機制。