Professional Documents
Culture Documents
Android ContentProvider
Android ContentProvider
Android ContentProvider
PROVIDERS
Content Provider
◻ A content provider makes a specific set of the application's
data available to other applications.
<standard_prefix>://<authority>/<data_path>/<id
>
examples:
content://media/internal/images = list of all internal images on device
content://media/external/images = list of all external images on device
content://call_log/calls = list of all calls in Call Log
content://browser/bookmarks = list of bookmarks
Basic Idea – example for Contacts
Content Provider
◻ Data mapped to a URI (CONTENT_URI)
◻ URI is used to access data by client
◻ content://contacts/people/
■ All contact names
■ content://contacts/people/23
■ Contact with _ID = 23
CONTENT_URI
◻ Content URI can be complex
◻ Rather than hardcoding exact URI everywhere
◻ Content Providers expose the base URI as static
field NameProvider.CONTENT_URI
OPTION 1:
<TextView
<ListView
android:id="@+id/contactID"
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:layout_weight="1"
android:stackFromBottom="false"
</LinearLayout>
android:transcriptMode="normal" />
Example --- main Activity Class
public class ProviderActivity extends ListActivity { c = cursorLoader.loadInBackground();
/** Called when the activity is first created. */
@Override String[] columns = new String[] {
public void onCreate(Bundle savedInstanceState) { ContactsContract.Contacts.DISPLAY_NAME,
super.onCreate(savedInstanceState); ContactsContract.Contacts._ID};
setContentView(R.layout.main);
Uri allContacts = ContactsContract.Contacts.CONTENT_URI; int[] views = new int[] {R.id.contactName, R.id.contactID};
String[] projection = new String[]
{ContactsContract.Contacts._ID, SimpleCursorAdapter adapter;
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER}; adapter = new SimpleCursorAdapter( this, R.layout.main,
c, columns, views,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
Cursor c;
CursorLoader cursorLoader = new CursorLoader( this,
this.setListAdapter(adapter);
allContacts, projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?“,
PrintContacts(c);
new String[] {"%Lee"},
}
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
Example --- main Activity Class
continued
private void PrintContacts(Cursor c)
{
if (c.moveToFirst()) {
do{
<<< UTILITY Method to print
String contactID = c.getString(c.getColumnIndex( out the result set returned in
ContactsContract.Contacts._ID));
String contactDisplayName =
the Cursor instance c that
c.getString(c.getColumnIndex( contains all the Contacts.
ContactsContract.Contacts.DISPLAY_NAME));
Log.v("Content Providers", contactID + ", " +
contactDisplayName);
} while (c.moveToNext());
}
}
}
Example ---some explanation
◻ Predefined Query String Constants
Uri allContacts = ContactsContract.Contacts.CONTENT_URI
SAME AS
Uri allContacts = Uri.parse(“content://contacts/people”);
Example ---some explanation
◻ Following is like saying give me all the contacts with the columns ID,
DISPLAY_NAME and HAS_PHONE_NUMBER where the DISPLAY_NAME
is Like Lee and orderby DISPLAY_NAME in Ascending order (ASC)
Cursor c;
CursorLoader cursorLoader = new CursorLoader( this,
allContacts, projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?“, new String[] {"%Lee"},
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
Predefined Query Strings
◻ Predefined Query String Constants
Uri allContacts = ContactsContract.Contacts.CONTENT_URI
SAME AS
Uri allContacts = Uri.parse(“content://contacts/people”);
◻ Other examples
◻ Browser.BOOKMARKS_URI
◻ Browser.SEARCHES_URI
◻ CallLog.CONTENT_URI
◻ MediaStore.Images.Media.INTERNAL_CONTENT_URI
◻ MediaStore.Images.Media.EXPERNAL_CONTENT_URI
◻ Settings.CONTENT_URI
Another option to query Content
Provider
Older method