Tutorial details

Android beginner tutorial: Part 67 - Creating a Content Provider | App Code for Sale | Preview

Android beginner tutorial: Part 67 - Creating a Content Provider | Android Tutorial

Content Providers and their creation process

Overview PAGE TOP

Today we'll learn about Content Providers and their creation process.

A Content Provider is a component that allows you to store, manage and share data between Android applications. It is declared the same way Activities, Services and BroadcastReceivers are declared. We need to create a class extending ContentProvider, as well as add it to the AndroidManifest.xml file.

Usually the first step when adding components is to add them to the manifest, but this time we will first learn some general information about content providers, before actually adding them anywhere.

The Android SDK library includes a ContentProvider class, which, if you want to create a provider, you need to extend in your own class. The class has 5 methods that you need to override (4 are required, the last one is optional) - query(), insert(), update(), delete() and getType().

The query() method is used for returning data from a database, insert() is used for inserting new rows to the Content Provider, update() is used to update already existing records, delete() deletes data and getType() returns MIME data type in the Content Provider.

Eclipse IDE can auto-generate the class, adding all the necessary (empty) methods:

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;


public class myContentProvider extends ContentProvider {

public myContentProvider() {
// TODO Auto-generated constructor stub
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}

@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
return null;
}

@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}

}

Each ContentProvider provides an open URI object, that identifies its set of data. A ContentProvider that has multiple sets of data (multiple tables) need to provide separate URI for each set. URIs are usually declared along with ContentProviders.

ContentProvider URIs consist of 4 parts. An example of a ContentProvider URI:

content://com.kircode.codeforfoodtest/contacts/10

The first part, the prefix, is "content://". It never changes.

The second part is the package and the name of the class (made that way to be sure that it's a unique address). In the example above this part is "com.kircode.codeforfoodtest".

The third part is the path that Content Provider uses to find the needed set of data. If Content Provider only has a single type of data (one table), this part is not necessary to include. It is also possible to have subtypes like "contacts/photos" and "contacts/phones".

The last part is the id of an individual row in the table. This is the _ID of the record. If the query isn't limited to a single row, then this part is left blank.

When a query is sent, the system checks the URI and directs the query to the needed Content Provider. The Content Provider itself is not used directly - there's a special class for that called ContentResolver.

A ContentResolver instance can be received using the getContentResolver() function inside an Activity or another Android component:

ContentResolver myResolver = getContentResolver();

This class has a lot of methods for working with database contents. Using a ContentResolver object you can interact with any available Content Providers.

The URI value is used in all interactions with Content Provider. Each ContentResolver method receives an URI as its first parameter, since that's the path to the Content Provider that you want to work with.

As I said before, it is needed to declare the URI of your custom ContentProvider object in the class. It can be done like this:

static final Uri CONTENT_URI = Uri.parse("content://com.kircode.codeforfoodtest/contacts/10");

That is all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-67.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.