Crazy Brush Android Example 5: Read the phone Address Book description
The Mad Brush Android sample series has opened. Each learning an Android paradigm, a single example generates a running app and a brief analysis of key source code. Then provide the packaged source code download.
Function
Provide complete code to read the contents of the phone address book through Contenresolver.
The code package is here, without the download points:
http://download.csdn.net/detail/logicteamleader/8806135
Source
Examples come from the com.example.android.apis.content.PickContact of Android-20.
Environment
Code Runtime Environment:
1.adt2014 version;
2.android:minsdkversion= "8"; android:targetsdkversion= "20"
The APPCOMPATV7 has been generated in 3.workspace, and its version is android-22;
Code
This example has two important technical points:
The first knowledge point, which, by setting the action and type of intent, uses an implicit way to launch the relevant app to read the contents of the phone. The ACTION used here is intent.action_get_content, meaning to read various content on the phone. There are four of the type here, respectively:
1. ContactsContract.Contacts.CONTENT_ITEM_TYPE
2. "Vnd.android.cursor.item/person"
3. ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
4. ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
Its true values are:
1. "Vnd.android.cursor.item/contact"
2. "Vnd.android.cursor.item/person"
3. "Vnd.android.cursor.item/phone_v2"
4. The "Vnd.android.cursor.item/postal-address_v2"
represents the type of four content for contacts, people, phones, and addresses.
based on the combination of intent.action_get_content and these four types, the intent can be started to obtain these four kinds of content, and in the void Onactivityresult (int requestcode, int ResultCode, Intent data) function is returned through the data variable.
The second knowledge point, through contentresolver, queries the desired content from the returned intent. The method used is cursor Android.content.ContentResolver.query (URI Uri, string[] projection, String selection, string[] Selectionargs, String SortOrder) with the following parameters:
1. The URI is the URI required for the query, and its schema must be content://;
2. Projection is an array of query result items. If you enter NULL, all items are returned;
3. Selection is a conditional statement, as in the SQL language where clause;
4. Selectionargs is an array of true values for the conditional statement;
5. SortOrder is a sort parameter. Like an ORDER BY clause in the SQL language. The
query returns a cursor and then extracts the desired value from the cursor by means of getint or getstring.
In this example, only the ID and name two values are removed, to get the other values, set the projection parameter and read from the cursor.
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "Licen Se "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageCom.example.pickcontact;//need the following import to get access to the app resources, since this//class is in a sub-package.Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.provider.BaseColumns;ImportAndroid.provider.ContactsContract;ImportAndroid.provider.ContactsContract.CommonDataKinds.Phone;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;/** * Demonstrates launching the Contacts app to pick a contact. Does not require * permission to read contacts, as that permission would be granted when the * selected contact is returned . */ Public class pickcontact extends Activity {Toast Mtoast; Resultdisplayer Mpendingresult; Class Resultdisplayer implements Onclicklistener {String mmsg; String Mmimetype; Resultdisplayer (String msg, string mimeType) {mmsg = msg; Mmimetype = MimeType; } Public void OnClick(View v) {//By setting intent's action to action_get_content, and setting type to Mmimetype //Start the app on your phone to read contacts, read the Address book contentIntent Intent =NewIntent (intent.action_get_content); Intent.settype (Mmimetype); Mpendingresult = This; Startactivityforresult (Intent,1); } }@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.pick_contact);//Watch for button clicks.(Button) Findviewbyid (r.id.pick_contact)). Setonclicklistener (NewResultdisplayer ("Selected Contact", ContactsContract.Contacts.CONTENT_ITEM_TYPE)); (Button) Findviewbyid (R.id.pick_person)). Setonclicklistener (NewResultdisplayer ("Selected person","Vnd.android.cursor.item/person")); (Button) Findviewbyid (R.id.pick_phone)). Setonclicklistener (NewResultdisplayer ("Selected Phone", ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)); (Button) Findviewbyid (r.id.pick_address)). Setonclicklistener (NewResultdisplayer ("Selected Address", ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)); }@Override protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {if(Data! =NULL{URI uri = Data.getdata ();if(URI! =NULL) {Cursor c =NULL;Try{//For more information, please inquire and set the projection parameterstring[] Projection =NewString[] {basecolumns._id, phone.display_name}; c = getcontentresolver (). Query (URI, projection,NULL,NULL,NULL);if(c! =NULL&& C.movetofirst ()) {intid = c.getint (0); String name = C.getstring (1);if(Mtoast! =NULL) {mtoast.cancel (); } String txt = mpendingresult.mmsg +": \ n"+ URI +"\nid:"+ id+"\nname:"+name; Mtoast = Toast.maketext ( This, TXT, toast.length_long); Mtoast.show (); } }finally{if(c! =NULL) {c.close (); } } } } }}
Crazy Brush Android Example 5: Read the phone Address Book