內容供應器
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
內容供應器可協助應用程式管理自行儲存資料或由其他應用程式儲存的資料存取權,並提供與其他應用程式共用資料的方式。這些 API 能封裝資料,並提供定義資料安全性的機制。內容供應器是標準介面,可將某項程序中的資料與在其他程序中執行的程式碼連結在一起。
實作內容供應器有許多好處。最重要的是,您可以設定內容供應器,讓其他應用程式安全地存取及修改應用程式資料,如圖 1 所示。
圖 1 內容供應器管理儲存空間存取權的總覽圖表。
如果您打算分享資料,請使用內容供應器。如果您不打算共用資料,可以不使用資料,但您可以選擇這樣做,因為它們提供抽象化機制,可讓您修改應用程式資料儲存空間實作,而不影響其他需要存取您的資料的應用程式。
在這種情況下,只有內容供應器會受到影響,不影響存取該內容的應用程式。例如您可以更換 SQLite 資料庫以進行替代儲存空間,如圖 2 所示。
圖 2. 插圖:遷移內容供應器儲存空間。
許多其他類別依賴 ContentProvider
類別:
如果您使用上述任一類別,就必須在應用程式中實作內容供應器。使用同步轉換介面架構時,您也可以建立虛設常式內容供應器做為替代方案。詳情請參閱「建立虛設常式內容供應器」。此外,在下列情況下,您需要自己的內容供應器:
- 在應用程式中實作自訂搜尋建議。
- 向小工具公開應用程式資料。
- 從您的應用程式複製複雜的資料或檔案,並貼到其他應用程式。
Android 架構包含管理音訊、影片、圖片和個人聯絡資訊等資料的內容供應器。您可以在 android.provider
套件的參考說明文件中查看其中幾項設定。但在某些情況下,所有 Android 應用程式都可以存取這些供應器。
內容供應器可用來管理各種資料儲存來源的存取權,包括結構化資料 (例如 SQLite 關聯資料庫) 或非結構化資料 (例如圖片檔)。如要進一步瞭解 Android 可用的儲存空間類型,請參閱「資料與檔案儲存空間總覽」和「
設計資料儲存空間」。
內容供應器的優點
內容供應器提供精細的控制選項,讓你控管資料存取權限。您可以選擇僅存取應用程式中的內容供應器、授予大量存取其他應用程式的資料權限,或是設定不同的讀取和寫入資料權限。如要進一步瞭解如何安全地使用內容供應器,請參閱
資料儲存的安全性提示和
內容供應器權限。
您可以使用內容供應器來簡化在應用程式中存取不同資料來源的詳細資料。例如,您的應用程式可能會將結構化記錄儲存在 SQLite 資料庫,以及影片和音訊檔案。您可以使用內容供應器存取所有資料。
此外,CursorLoader
物件仰賴內容供應器執行非同步查詢,然後將結果傳回應用程式的 UI 層。如要進一步瞭解如何使用 CursorLoader
在背景載入資料,請參閱「
載入器」。
以下主題會進一步說明內容供應者:
-
內容供應器基礎知識
- 如何使用現有的內容供應器存取及更新資料。
-
建立內容供應器
-
如何設計及實作自己的內容供應器。
-
日曆提供者總覽
-
如何存取 Android 平台中的日曆供應程式。
-
聯絡人提供者
-
如何存取 Android 平台的聯絡人供應程式。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Content providers can help an application manage access to data stored by itself or\nstored by other apps and provide a way to share data with other apps. They encapsulate the\ndata and provide mechanisms for defining data security. Content providers are the standard\ninterface that connects data in one process with code running in another process.\n\n\nImplementing a content provider has many advantages. Most importantly, you can configure a\ncontent provider to let other applications securely access and modify your app data,\nas illustrated in figure 1.\n\n\n**Figure 1.** Overview diagram of how content providers\nmanage access to storage.\n\n\nUse content providers if you plan to share data. If you don't plan to share data,\nyou don't have to use them, but you might choose to because they provide an abstraction\nthat lets you make modifications to your application data storage\nimplementation without affecting other applications that rely on access to your data.\n\n\nIn this scenario, only your content provider is affected and not the applications that\naccess it. For example, you might swap out a SQLite database for alternative storage, as\nillustrated in figure 2.\n\n\n**Figure 2.** Illustration of migrating content provider storage.\n\n\nA number of other classes rely on the [ContentProvider](/reference/android/content/ContentProvider) class:\n\n- [AbstractThreadedSyncAdapter](/reference/android/content/AbstractThreadedSyncAdapter)\n- [CursorAdapter](/reference/android/widget/CursorAdapter)\n- [CursorLoader](/reference/android/content/CursorLoader)\n\n\nIf you use any of these classes, you need to implement a content provider\nin your application. When working with the sync adapter framework you can also create\na stub content provider as an alternative. For more information, see\n[Create a stub content\nprovider](/training/sync-adapters/creating-stub-provider). In addition, you need your own content provider in the following cases:\n\n- To implement custom search suggestions in your application.\n- To expose your application data to widgets.\n- To copy and paste complex data or files from your application to other applications.\n\n\nThe Android framework includes content providers that manage data such as audio, video, images,\nand personal contact information. You can see some of them listed in the reference\ndocumentation for the\n[android.provider](/reference/android/provider/package-summary)`\n` package. With some restrictions, these providers are accessible to any Android\napplication.\n\n\nA content provider can be used to manage access to a variety of data storage sources, including\nboth structured data, such as a SQLite relational database, or unstructured data such as image\nfiles. For more information about the types of storage available on Android, see the\n[Data and file storage overview](/guide/topics/data/data-storage) and\n[Design data storage](/guide/topics/providers/content-provider-creating#DataStorage).\n\nAdvantages of content providers\n-------------------------------\n\n\nContent providers offer granular control over the permissions for accessing data. You can\nchoose to restrict access to only a content provider that is within your application, grant\nblanket permission to access data from other applications, or configure different permissions\nfor reading and writing data. For more information about using content providers securely, see the\n[security tips for data storage](/privacy-and-security/security-tips#StoringData) and\n[Content provider permissions](/guide/topics/providers/content-provider-basics#Permissions).\n\n\nYou can use a content provider to abstract away the details for accessing different data\nsources in your application. For example, your application might store structured records in a\nSQLite database, as well as video and audio files. You can use a content provider to access all\nof this data.\n\n\nAlso, [CursorLoader](/reference/android/content/CursorLoader) objects rely on content providers to run\nasynchronous queries and then return the results to the UI layer in your application. For more\ninformation about using a `CursorLoader` to load data in the background, see\n[Loaders](/training/load-data-background/setup-loader).\n\n\nThe following topics describe content providers in more detail:\n\n\n**[Content provider basics](/guide/topics/providers/content-provider-basics)**\n:\n How to access and update data using an existing content provider.\n\n\n**[Create a content provider](/guide/topics/providers/content-provider-creating)**\n:\n How to design and implement your own content provider.\n\n\n**[Calendar provider overview](/guide/topics/providers/calendar-provider)**\n:\n How to access the Calendar Provider that is part of the Android platform.\n\n\n**[Contacts Provider](/guide/topics/providers/contacts-provider)**\n:\n How to access the Contacts Provider that is part of the Android platform."]]