CONTENT PROVIDER
Content
providers store and retrieve data and make it accessible to all applications. They're the only way to share data across
applications; there's no common storage area that all Android packages can
access. Android ships with a number of content providers for common data types
(audio, video, images, personal contact information, and so on). You can see
some of them listed in the android.provider package. You can query these providers for
the data they contain (although, for some, you must acquire the proper
permission to read the data).
If you want to make your own data public, you
have two options: You can create your own content provider (a ContentProvider subclass) or you can add the data to an existing
provider if there's one that controls the same type of data and you have
permission to write to it.
9.1 CONTENT PROVIDER BASICS
How a content provider actually stores its
data under the covers is up to its designer. But all content providers implement
a common interface for querying the provider and returning results — as well as
for adding, altering, and deleting data.
It's an interface that clients use
indirectly, most generally through ContentResolver objects. You get a ContentResolver by
calling getContentResolver() from within the implementation of an
Activity or other application component:
ContentResolver
cr = getContentResolver();
|
Content
providers expose their data as a simple table on a database model, where each
row is a record and each column is data of a particular type and meaning. For
example, information about people and their phone numbers might be exposed as
follows:
ID
|
NUMBER
|
NUMBER_KEY
|
LABEL
|
NAME
|
TYPE
|
13
|
(425) 555 6677
|
425 555 6677
|
Kirkland office
|
Bully Pulpit
|
TYPE_WORK
|
44
|
(212) 555-1234
|
212 555 1234
|
NY apartment
|
Alan Vain
|
TYPE_HOME
|
45
|
(212) 555-6657
|
212 555 6657
|
Downtown office
|
Alan Vain
|
TYPE_MOBILE
|
53
|
201.555.4433
|
201 555 4433
|
Love Nest
|
Rex Cars
|
TYPE_HOME
|
Every record includes a numeric
_ID
field that uniquely identifies the record within
the table. IDs can be used to match records in related tables — for example, to
find a person's phone number in one table and pictures of that person in
another.
A query returns a Cursor
object that can move from record to record and column to column to read the
contents of each field. It has specialized methods for reading each type of
data. So, to read a field, you must know what type of data the field contains.
9.2 URIs
If
you're defining a content provider, it's a good idea to also define a constant
for its URI, to simplify client code and make future updates cleaner. Android
defines
CONTENT_URI
constants
for all the providers that come with the platform. For example, the URI for the
table that matches phone numbers to people and the URI for the table that holds
pictures of people (both controlled by the Contacts content provider) are: android.provider.Contacts.Phones.CONTENT_URI android.provider.Contacts.Photos.CONTENT_URI |
The URI
constant is used in all interactions with the content provider. Every ContentResolver method takes the URI as its first argument. It's what identifies which
provider the ContentResolver should talk to and which table of the provider is
being targeted.
Using a Content
Provider
Here are some of Android's most useful built-in content providers
Here are some of Android's most useful built-in content providers
Content Provider
|
Intended Data.
|
Contacts
|
Contact
details.
|
Browser
|
Browser
bookmarks, browser history,
etc.
|
Call
Log
|
Missed calls,
call details, etc.
|
Media
Store
|
Media
files such as audio, video and
images.
|
Settings
|
Device
settings and preferences.
|
An example to display, display name from contacts
package a.b.c;
import java.util.ArrayList;
import android.app.ListActivity;
import
android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import
android.provider.ContactsContract;
import
android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.ArrayAdapter;
import android.widget.Button;
public class
TrialcontentActivity extends ListActivity {
Button b1,b2;
ArrayList<String> results = new
ArrayList<String>();
ArrayAdapter<String> arraydpter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arraydpter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,results);
b1 = (Button)findViewById(R.id.bttn1);
b1.setOnClickListener(new OnClickListener() {
public void
onClick(View arg0) {
// TODO Auto-generated method stub
ContentResolver cr= getContentResolver();
System.out.println("1");
Cursor people =
cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(people.moveToNext())
{
System.out.println("2");
int nameFieldColumnIndex =
people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact =
people.getString(nameFieldColumnIndex);
results.add(contact);
}
people.close();
setListAdapter(arraydpter);
arraydpter.notifyDataSetChanged();
}
});
b2 = (Button)findViewById(R.id.bttn2);
b2.setOnClickListener(new OnClickListener() {
public void
onClick(View arg0) {
// TODO Auto-generated method stub
results.remove(0);
arraydpter.notifyDataSetChanged();
}
});
}
}
|
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bttn1"
android:text="Click to get
contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/bttn2"
android:text="Click to
delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
|
Manifest
file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.b.c"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:label="@string/app_name"
android:name=".TrialcontentActivity"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>
|
Note that in
this case we need to add permission to access contacts from phone.
No comments:
Post a Comment