11.1C: Sharing content with other apps
- What you should already KNOW
- What you will LEARN
- What you will DO
- App overview
- Task 1. Make your content provider available to other apps
- Related concept
- Learn more
To protect app and user data, apps cannot share data with other apps directly. However, apps can make data available to other apps by using a content provider. Client apps can then use a content resolver to access the data via the content provider's public interface.
The following diagram shows how a hat wholesaler might use a content provider to share information about its inventory to apps that sell hats.
In this practical you will modify WordListSQLWithContentProvider to allow other apps to access the data in its content provider. Then you will create a second app, WordListClient, that has no data of its own, but instead, fetches data from WordListSQLWithContentProvider's content provider.
What you should already KNOW
For this practical you should be familiar with:
- Content providers and resolvers
- How to rename packages
- The WordListSQLWithContentProvider app from the previous practical
What you will LEARN
You will learn how to:
- Set permissions, so other apps can use your app's content provider.
- Build a client app that fetches data from your app's content provider.
What you will DO
- Enable WordListSQLWithContentProvider to share its data.
- Create a client app that gets data from the content provider of WordListSQLWithContentProvider.
You will use two apps in this practical.
- The existing WordListSQLWithContentProvider app that you built in the previous practical.
- A new WordListClient app that will query the content provider of WordListSQLWithContentProvider. The UI for this app is the same as WordListInteractive.
Task 1. Make your content provider available to other apps
By default, apps cannot access the data of other apps.
To make your content provider available to other apps, you must specify permissions in the AndroidManifest of your app. This is true for any app that has a content provider. Each content provider needs permissions specified in its AndroidManifest.
Permissions are not covered in detail in these practicals. You can learn more in Implementing Content Provider Permissions.
1.1. Modify WordListWithContentProvider to allow apps access
- Open WordListSQLWithContentProvider in Android Studio.
- Open the AndroidManifest.xml file.
- Add an export statement inside the
At the top level, inside the
<manifest>tag add a permission for the content provider.
It is good practice to use your unique package name in order to keep the permission unique.
<permission android:name="com.android.example.wordlistsqlwithcontentprovider.PERMISSION" />
Run the app to make sure there are no errors, and leave it installed on the device.
In order for another app to access WordListWithContentProvider's content provider, the app with the content provider has to be installed on the device. It is not necessary for it to be running.
You now have a content provider on your device that another app can access. Next, you are going to build an app, WordListClient, that gets words from the content provider and displays them.
1.2. Create the WordListClient app
Instead of building a client app from scratch, you will create WordListClient from a copy of WordListSQLWithContentProvider. You will keep the user interface and the adapter to display the data. You will remove the content provider and the database, and instead get data from the content provider of WordListSQLWithContentProvider.
- Create a copy of the WordListSQLWithContentProvider folder and call it WordListClient.
- Open the copied app in the WordListClient folder in Android Studio.
- Rename the package (Refactor > Rename) to wordlistclient.
- Open build.gradle(Module:app) and change the app id to wordlistclient.
- In strings.xml, change the app name to WordListClient.
- In the Android Manifest of WordListClient, remove the
<provider>declaration as there won't be a provider in WordListClient.
- At the top level, inside the
<manifest>tag, add a
<uses-permission>permission to use the content provider of WordListSQLWithContentProvider.
<uses-permission android:name = "com.android.example.wordlistsqlwithcontentprovider.PERMISSION"/>
- Delete the WordListContentProvider class, because the app will access the content provider of WordListSQLWithContentProvider.
- Delete the WordListOpenHelper class, because your app does not need a database or an open helper of its own.
- Look at MainActivity and WordListAdapter. Note that the code for inserting, deleting, and updating words remains unchanged, calling the content provider of WordListSQLWithContentProvider using the URIs specified in the Contract class.
- Run WordListClient.
- In spite of not having data of its own, WordListClient displays data, which is the data it fetches from the WordListSQLWithContentProvider app's content provider.
- Insert a few words on WordListClient.
- Start WordListSQLWithContentProvider.
- Notice that the word you inserted with WordListClient, displays also in WordListSQLWithContentProvider, because they share one data source.
- Delete the word in WordListSQLWithContentProvider and notice the word also being deleted from the display of WordListClient. (You may need to scroll to make the change appear.)
- As you interact with other the other app, changes made by one app are reflected in the other app.
In the previous hat store example, the warehouse owner can update the hat inventory with new red or fancy hats, and the store apps will immediately be able to show these new hats to their customers. And if the red hat store sells all the red fancy hats, the fancy hat store will know that the inventory of fancy red hats is sold out.
Android Studio project: WordListClient
- Your app can permit other apps to interact with its content provider and get or edit data.
- You can build a client app that instead of providing it's own data, fetches data from another app's content provider.
- You can separate management of the data from displaying the data. That is, you can have one app that manages the content provider, and many client apps that use the data provided by the content provider.
The related concept documentation is in Android Developer Fundamentals: Concepts.