Before doing a little exercise need to get the phone number of contact person, by looking at the relevant information, I try to write a small example, can be successfully obtained.
First, you need to get permissions in Androidmanifest.xml
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
XML layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:orientation= "vertical" >
<textview
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:text= "phone number is:"/> <edittext android:id= "
@+id/editnumber"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:editable= " False "/>
<button
android:id=" @+id/buttonquary "
android:layout_width=
" Wrap_content " android:layout_height= "Wrap_content"
android:text= "query Contacts"/>
</LinearLayout>
Mainactivity.java
Package com.getcontacts;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.app.Activity;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
public class Mainactivity extends Activity {button button;
EditText EditText;
Private Uri Uri_data = Uri.parse ("Content://com.android.contacts/data");
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button = (button) Findviewbyid (r.id.buttonquary);
EditText = (edittext) Findviewbyid (R.id.editnumber); Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View arg0) {//TODO auto-generated method Stub Edittext.settext (""); Clear the phone number before doing a jump Intent Intent = new Intent (intent.action_picK, Android.provider.ContactsContract.Contacts.CONTENT_URI);
Startactivityforresult (Intent, 1);
}
}); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generate
D method Stub Super.onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = 1) {//Request code is 1 log.i ("info", "Resultadd");
String strnumber = "";
if (data!= null) {//To determine whether the returned intent is an empty URI uri = Data.getdata (); LOG.I ("Info", uri.tostring ());
In log print out gets the URI Cursor Cursor = getcontentresolver (). Query (URI, NULL, NULL, NULL, NULL);
while (Cursor.movetonext ()) {String Strid = cursor.getstring (Cursor.getcolumnindex ("name_raw_contact_id")); Cursor Cursor2 = Getcontentresolver (). query (Uri_data, NULL, raw_contact_id = "+ Strid +" and Mimet
ype_id = 5 ", NULL, NULL); if (Cursor2.movetofirst ()) {strnumber = Cursor2.getstring (cuRsor2.getcolumnindex ("data1"));
Strnumber = Strnumber.replace ("", "");
Strnumber = Strnumber.replace ("-", "");
} cursor2.close ();
} cursor.close ();
Edittext.settext (Strnumber);
}
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.