Android Content Providers and Data Access

cs499 mobile application development n.w
1 / 16
Embed
Share

Dive into the world of Android content providers, data storage, and access methods. Learn how to use Cursor objects to retrieve and manipulate data, query information, and efficiently interact with various content sources within your Android application.

  • Android
  • Content Providers
  • Data Access
  • Programming
  • Mobile Development

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  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, String[] projection String selection String[] selectionArgs // SQL pattern args String sortOrder ) // ContentProvider URI // Columns to retrieve // SQL selection pattern // 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. public class ContactsEditActivity extends ListActivity { private Cursor mContacts; @Override 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<?> parent, View v, int position, long id) { if (mContacts.moveToPosition(position)) { int selectedId = mContacts.getInt(0); // _ID Column // gather email data from the tables Cursor email = getContentResolver().query( CommonDataKinds.Email.CONTENT_URI, new String[] {CommonDataKinds.Email.DATA }, ContactsContract.Data.CONTACT_ID + " = " + selectedId, null, null); Cursor phone = getContentResolver().query( CommonDataKinds.Phone.CONTENT_URI,new String[] {CommonDataKinds.Phone.NUMBER }, ContactsContract.Data.CONTACT_ID + " = " + selectedId, null, null); // Build the Dialog message StringBuilder sb = new StringBuilder(); sb.append(email.getCount() + " Emails\n"); if (email.moveToFirst()) { do { sb.append("Email: " + email.getString(0)); sb.append('\n'); } while (email.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()); sb.append('\n'); } // 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(); email.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() { getContentResolver().delete( ContactsContract.RawContacts.CONTENT_URI, 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 http://developer.android.com/guide/topics/pr oviders/content-providers.html http://developer.android.com/guide/topics/pr oviders/contacts-provider.html

Related


More Related Content