Creating a Custom Content Provider

Android uses Content Providers to mitigate access to data. You used a Content Provider in Chapter 9 to access and read coordinate information from a GPS. The same process applies to databases. There are Content Providers for Contact Lists, IMs, and Recent Calls. However, there is not yet a Content Provider for your Friends database.

Android is extremely flexible and allows you to create your own custom Content Providers for your own custom data. In this section you will create a Content Provider that works with your Friends database. This will be the key to access the friend data and eventually display it to the screen.

In the next section you will edit the strings.xml file. This file holds some global string content that can be used throughout your Activity.

First, you will edit the strings.xml file for your project. The strings.xml file is created with each project but you have not used it yet. This file holds static strings that can be used in your Activities.

Typically, you will not be able to determine all of the strings that you need to use in your Activity before you even write it. That is, you will usually add entries to strings.xml as you build the Activity. However, because that would break the flow of the book, I am giving you the full contents of the strings.xml file up front. Edit your strings.xml file to look as follows:

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">FindAFriend</string> <string name="menu_delete">Delete</string> <string name="menu_insert">Add Friend</string> <string name="find_friends">Find Friends</string> <string name="menu_revert">Revert</string> <string name="menu_discard">Discard</string> <string name="resolve_edit">Edit location</string> <string name="resolve_title">Edit name</string> <string name="title_create">Create Friend</string> <string name="title_edit">Edit Friend</string> <string name="title_notes_list">Friends</string> <string name="title_note">Location</string> <string name="title_edit_title">Friend Name:</string> <string name="button_ok">OK</string> <string name="error_title">Error</string>

<string name="error_message">Error loading note</string> </resources>

With the strings.xml file complete, you need to create a .java file to hold your code. You should call this file FriendsProvider.java. You also need to create another .java file to hold your data definition. Name this file Friends.java, because it will define what a Friends data structure looks like and let your Content Provider access it correctly. (Because the provider will be a class that sits in your project, there is no need to build a corresponding .xml layout file.)

Technically, your custom Content Provider does not need to reside within the same project or package as the rest of the code for this application. For simplicity's sake, I am making the Content Provider a class in the FindAFriend project. However, if you plan to create a Content Provider that may be used by multiple projects, create it in a separate package. This will let you call one package when you want to use the Content Provider only.

Let's start with the Friends.java file. You need to import only two packages for this relatively small class:

import android.net.Uri;

import android.provider.BaseColumns;

BaseColumns will be implemented by a subclass off of your main Friends class. Name this subclass Friend, because it will represent one friend from the Friends dataset. The following code shows how you should set up the class outline:

public final class Friends {

public static final class Friend implements BaseColumns {

This class will hold some static variables that define each of the columns in your Friends database, the Content URI, and the default sort order for the records.

A Content URI is used to identify the content that you will handle. This value must be unique.

The strings that you need to define look like this:

public static final Uri CONTENT_URI Uri.parse("content://android_programmers_guide.FindAFriend.Friends/friend");

public

static

final

String

DEFAULT_SORT_ORDER = "modified DESC

public

static

final

String

NAME = "name";

public

static

final

String

LOCATION = "location";

public

static

final

String

CREATED_DATE = "created";

public

static

final

String

MODIFIED_DATE = "modified";

With these variables set, the contents of your Friends class come together perfectly. The full file should look like this:

package android_programmers_guide.FindAFriend;

import android.net.Uri;

import android.provider.BaseColumns;

public final class Friends {

public static final class Friend implements BaseColumns { public static final Uri CONTENT_URI

Uri.parse("content://android_programmers_guide.FindAFriend.Friends/friend");

public

static

final

String

DEFAULT_SORT_ORDER = "modified

public

static

final

String

NAME = "name";

public

static

final

String

LOCATION = "location";

public

static

final

String

CREATED_DATE = "created";

public

static

final

String

MODIFIED_DATE = "modified";

In the next section you will create your Content Provider.

0 0

Responses

  • ASMARINA
    How to create my own content providers in android?
    7 years ago

Post a comment