Android simple ContentProvider and ContentResolver example

In Android security model, one application cannot directly access (read/write) other application's data. Every application has its own id data directory and own protected memory area.
ContentProvider is the best way to share data across applications. Content provider is a set of data wrapped up in a custom API to read and write. Applications/Processes have to register themselves as a provider of data. Other applications can request Android to read/write that data through a fixed API.
Content provider API adheres to CRUD principle.
Examples for ContentProvider are Contacts-that exposes user information to other applications, Media store-Allows other applications to access,store media files.
Okey, in this tip, I present the way to use system Content Provider (contacts content provider) to get, add new and delete contacts in Android device through an Uri.

1. Launch Eclipse and start a new Android Project (min-sdk I used is 14).
2. Create a layout (activity_main.xml):
3. Create a simple model (ContactInfor) to use in project:
4. Give a custom adapter for ListView in our activity (ContactAdapter - extends ArrayAdapter):
5. Now, in running activity (ContactActivity), we provide 3 methods to add a new contact, delete an exist contact and get all contacts in our device. By performing this, ContentResolver was used to access system contacts information.
- Add new contact method:
- Get all contacts in device:
- Delete an exist contact (I call this method in ContactAdapter) and update contacts list:
6. Add some necessary methods, we have full ActivityContact code:
7. Open your AndroidManifest.xml file and add Write/Read Contact permission:
8. Some screens after running app:

pic name pic name pic name pic name
(sorry for having ads)


Previous post
« Prev Post
Next post
Next Post »