Previously, we introduced Content about Content Provider. Here we will introduce the Content Provider defined by the system and the custom Content Provider in two sections.
Here we will introduce how to use the system-defined Content Provider: to read the contact information of the system through ContentResolver.
Step 1: Create a new project named readPeople. Because the project is displayed as a list, this class inherits from listavti.pdf instead of Activity;
Step 2: modify the java Source Code. The Code is as follows:
View plaincopy to clipboardprint? /** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. main );
// Obtain the cursor object
Cursor c = getContentResolver (). query (ContactsContract. Contacts. CONTENT_URI, null );
This. startManagingCursor (c );
// Instantiate the list Adapter
StAdapter adapter = new SimpleCursorAdapter (this, android. r. layout. simple_list_item_1, c, new String [] {Contacts. DISPLAY_NAME}, new int [] {android. r. id. text1 });
This. setListAdapter (adapter );
}
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. main );
// Obtain the cursor object
Cursor c = getContentResolver (). query (ContactsContract. Contacts. CONTENT_URI, null );
This. startManagingCursor (c );
// Instantiate the list Adapter
ListAdapter adapter = new SimpleCursorAdapter (this, android. r. layout. simple_list_item_1, c, new String [] {Contacts. DISPLAY_NAME}, new int [] {android. r. id. text1 });
This. setListAdapter (adapter );
} View plaincopy to clipboardprint? Step 3: Add the following content to the AndroidManifestxml file: <uses-permission android: name = "android. permission. READ_CONTACTS"/>
Step 3: Add the following content to the AndroidManifestxml file: <uses-permission android: name = "android. permission. READ_CONTACTS"/> Step 4: run the program to see the following content:
From the column chenlong12580