Download presentation
Presentation is loading. Please wait.
1
CS499 – Mobile Application Development
Fall 2013 Programming the Android Platform Content Providers
2
Content Providers Datastore and interface to contents, allowing information to be shared (queried, updated, added, deleted) across apps. Datastore is pointed at via a URI (Uniform Resource Identifier) The data type for each column in a provider is usually listed in its documentation. Use Cursor objects to process the data. Ex: Contact list, Settings, CallLog, UserWordDictionary … – all provided with Android
3
Other Standard Content Providers
UserDictionary.Words.CONTENT_URI (content://user_dictionary/words)
4
Using Content Providers: Contacts
Contact list set up to be available to any other app by using the correct URI (path) and methods. Complex data access via a Cursor Need to declare android.permission.READ_CONTACTS and android.permission.WRITE_CONTACTS in the manifest
5
Querying Use query() to retrieve data
Returns a Cursor instance for accessing results A Cursor is an iterator over a result set Cursor query( Uri uri, // ContentProvider URI String[] projection // Columns to retrieve String selection // SQL selection pattern String[] selectionArgs // SQL pattern args String sortOrder // Sort order )
6
Cursor Provides access to query results
SimpleCursorAdapter – can be used with ListView Some useful methods: boolean moveToFirst() boolean moveToNext() int getColumnIndex(String columnName) String getString(int columnIndex)
7
Contact List - 1
8
ContactList1 – on blackboard
public class ContactsEditActivity extends ListActivity { private Cursor public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Return all contacts ordered by name String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; // Get all contacts visible to the user mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, null, null, ContactsContract.Contacts.DISPLAY_NAME); // Display contacts in a ListView SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mContacts, new String[] {ContactsContract.Contacts.DISPLAY_NAME }, new int[] {android.R.id.text1}); setListAdapter(mAdapter); } ContactList1 – on blackboard
9
Contract Classes ContactsContract.Contacts.DISPLAY_NAME
A contract class defines constants that help applications work with the content URIs, column names, intent actions, and other features of a content provider. Contract classes are not included automatically with a provider; the provider's developer has to define them and then make them available to other developers.
10
Contact List 2 Listener queries for email and phone info
Creates an AlertDialog
11
public void onItemClick(AdapterView<
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { if (mContacts.moveToPosition(position)) { int selectedId = mContacts.getInt(0); // _ID Column // gather data from the tables Cursor = getContentResolver().query( CommonDataKinds. .CONTENT_URI, new String[] {CommonDataKinds. .DATA }, ContactsContract.Data.CONTACT_ID + " = " + selectedId, null, null); Cursor phone = getContentResolver().query( CommonDataKinds.Phone.CONTENT_URI, new String[] {CommonDataKinds.Phone.NUMBER }, // Build the Dialog message StringBuilder sb = new StringBuilder(); sb.append( .getCount() + " s\n"); if ( .moveToFirst()) { do { sb.append(" " + .getString(0)); sb.append('\n'); } while ( .moveToNext()); sb.append('\n'); } sb.append(phone.getCount() + " Phone Numbers\n"); if (phone.moveToFirst()) { do { sb.append("Phone: " + phone.getString(0)); sb.append('\n'); } while (phone.moveToNext()); } // Create the Dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(mContacts.getString(1)); // Display name builder.setMessage(sb.toString()); builder.setPositiveButton("OK", null); builder.create().show(); .close(); phone.close();
12
Deleting Use delete() to delete data
private void deleteContact(String name) { getContentResolver().delete( ContactsContract.RawContacts.CONTENT_URI, ContactsContract.Contacts.DISPLAY_NAME+”=?”, new String[] {name}); } private void deleteAllContacts() { null,null);
13
Add/Update Starting to move into db topics (next week)…
14
Creating Content Providers
You need to create a content provider if: Your app needs to offer complex data or files to other apps You want to allow users to copy complex data from your app to other apps Allows you to offer both file and “structured” data to other apps. You do not need a content provider to have a SQL database inside a particular app.
15
ContentProvider class
Six abstract methods query() - Retrieve data from your provider. insert() - Insert a new row into your provider. update() - Update existing rows in your provider. delete() - Delete rows from your provider. getType() - Return the MIME type corresponding to a content URI. onCreate() Initialize your provider. These methods (except onCreate() ) may be called from multiple apps so must be thread-safe. Use permissions in manifest to control what apps can access your data
16
URLS
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.