Browse DevX
Sign up for e-mail newsletters from DevX


Using and Implementing Content Providers in Android : Page 3

Although Android ships with several useful content providers, you can easily extend it and build your own.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Registering the Content Provider

To register your content provider with Android, modify the AndroidManifest.xml file by adding the


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package="net.learn2develop.CPExamples" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CPExampleActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="BooksProvider" android:authorities="net.learn2develop.provider.Books" /> </application> <uses-permission android:name="android.permission.READ_CONTACTS"> </uses-permission> </manifest>

Using the Books Content Provider

With the new content provider completed, you can test it. Start by adding two books into the content provider in the CPExampleActivity.java file:

//---add a book--- ContentValues values = new ContentValues(); values.put(BooksProvider.TITLE, "C# 2008 Programmer's Reference"); values.put(BooksProvider.ISBN, "0470285818"); Uri uri = getContentResolver().insert( BooksProvider.CONTENT_URI, values); //---add another book--- values.clear(); values.put(BooksProvider.TITLE, "Programming Sudoku"); values.put(BooksProvider.ISBN, "1590596625"); uri = getContentResolver().insert( BooksProvider.CONTENT_URI, values);

In the preceding code, note that because the sample content provider is in the same package, you can use the BooksProvider.TITLE and the BooksProvider.ISBN constants to refer to the "title" and "isbn" fields, respectively. If you were instead accessing this content provider from a different package, you would not be able to use these constants. In the latter case, you would need to specify the field name directly, like this:

ContentValues values = new ContentValues(); values.put("title", "ASP.NET 2.0: A Developer's Notebook"); values.put("isbn", "0596008120"); Uri uri = getContentResolver().insert( Uri.parse( "content://net.learn2develop.provider.Books/books"), values);

Also note that for external packages you need to refer to the content URI using the fully-qualified content URI:


To prove that the two books were added correctly, create a query and then print out each book's ID, title and ISBN using the Toast class:

Uri allTitles = Uri.parse( "content://net.learn2develop.provider.Books/books"); Cursor c = managedQuery(allTitles, null, null, null, "title desc"); if (c.moveToFirst()) { do{ Toast.makeText(this, c.getString(c.getColumnIndex( BooksProvider._ID)) + ", " + c.getString(c.getColumnIndex( BooksProvider.TITLE)) + ", " + c.getString(c.getColumnIndex( BooksProvider.ISBN)), Toast.LENGTH_LONG).show(); } while (c.moveToNext()); }

The preceding query will return results by title sorted in descending order.

To update a book's details, call the update() method using a content URI that indicates the book's ID:

ContentValues editedValues = new ContentValues(); editedValues.put(BooksProvider.TITLE, "Programming Sudoku (Apress)"); getContentResolver().update( Uri.parse( "content://net.learn2develop.provider.Books/books/2"), editedValues, null, null);

Use a similar content URI to delete a book:

getContentResolver().delete( Uri.parse( "content://net.learn2develop.provider.Books/books/2"), null, null);

To delete all books, simply omit the book's ID in your content URI:

getContentResolver().delete( Uri.parse( "content://net.learn2develop.provider.Books/books"), null, null);

That's it, really. You've seen how to use the built-in content providers in Android, specifically, the CallLog content provider. The Content provider design represents a great decision by Google because it allows applications to share data through a standard set of programming interfaces. And it's extensible: You can create your own custom content provider to share your data with other packages that works just like the built-in providers.

package net.learn2develop.CPExamples; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.CallLog.Calls; import android.util.Log; public class CPExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Uri allCalls = Uri.parse("content://call_log/calls"); Cursor c = managedQuery(allCalls, null, null, null, null); if (c.moveToFirst()) { do{ String callType = ""; switch (Integer.parseInt(c.getString( c.getColumnIndex(Calls.TYPE)))) { case 1: callType = "Incoming"; break; case 2: callType = "Outgoing"; break; case 3: callType = "Missed"; } Log.v("Content Providers", c.getString(c.getColumnIndex(Calls._ID)) + ", " + c.getString(c.getColumnIndex(Calls.NUMBER)) + ", " + callType) ; } while (c.moveToNext()); } } }

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Thanks for your registration, follow us on our social networks to keep up-to-date