Other Interesting Articles
How to Use Content Provider
Content Provider provides a standardized interface for accessing and manipulating data. Data is accessed via URI (Uniform Resource Identifier) defined by the Content Provider.
1. Accessing Data from Content Provider
To access data from the Content Provider, you need to use the appropriate ContentResolver and URI.
Example of accessing a contact list:
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(“Contact”, name);
}
cursor.close();
}
2. Adding, Modifying, or Deleting Data
You can also add, change, or delete data through Content Provider using ContentResolver.
Adding Data:
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.DISPLAY_NAME, “John Doe”);
Uri newContactUri = getContentResolver().insert(ContactsContract.Contacts.CONTENT_URI, values);
Changing Data:
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.DISPLAY_NAME, “Jane Doe”);
getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, values, null, null);
Deleting Data:
getContentResolver().delete(ContactsContract.Contacts.CONTENT_URI, null, null);
Examples of Content Provider Usage
1. Contact Application:
- Content Provider allows other apps to access the user’s contact list.
- Example: A messaging app uses a Content Provider to select the recipient’s contact.
2. Photo Gallery App:
- Content Provider allows other apps to access photos and videos stored on the device.
- Example: A social media app uses a Content Provider to select photos to upload.
3. Calendar App:
- Content Provider allows other apps to access schedules and events on the user’s calendar.
- Example: A reminder app uses a Content Provider to add events to the calendar.
7. Application Manifest: Glue That Holds Applications Together
Application Manifest is an XML file that serves as a “glue” that holds all the components of an Android app together. This file defines the structure of the application, including Activity, Service, Broadcast Receiver, and Content Provider, as well as the permissions required by the application.
The Important Role of Application Manifest
1. Defining Application Components:
- Activity: Defines the screen or user interface.
- Service: Define the background process.
- Broadcast Receiver: Defines the components that respond to system messages.
- Content Provider: Define the mechanism of data sharing.
Example of Activity definition in AndroidManifest.xml:
<activity android:name=“.MainActivity”>
<intent-filter>
<action android:name=“android.intent.action.MAIN” />
<category android:name=“android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
2. Declare Permission:
- Android apps require permission to access certain features, such as camera, location, or the internet.
- This permit was declared in AndroidManifest.xml.
Example of a permission declaration:
<uses-permission android:name=“android.permission.CAMERA” />
<uses-permission android:name=“android.permission.INTERNET” />