============ Problem Description ============
The code is as follows
package com.example.sysaction;import android.net.uri;import android.os.build;import android.os.bundle;import android.provider.contactscontract;import android.annotation.suppresslint; import android.annotation.targetapi;import android.app.activity;import Android.content.cursorloader;import android.content.intent;import android.database.cursor;import android.view.menu;import android.view.view;import android.view.view.onclicklistener;import android.widget.button;import android.widget.edittext;public class sysaction extends Activity {final int PICK_CONTACT = 0; @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); button bn = (Button) Findviewbyid (r.id.bn); Bn.setonclicklistener (New onclicklistener () {@ Overridepublic void onclick (VIEW V) {//  Todo auto-generated method stubintent intent = new intent (); Intent.setaction (intent.action_get_content); Intent.settype ("Vnd.android.cursor.item/phone"); Startactivityforresult (Intent,pick_contact);}});} @TargetApi (build.version_codes. Honeycomb) @SuppressLint ("Newapi") Public void onactivityresult (int requestcode, int Resultcode, intent data) {Super.onactivityresult (requestcode, resultcode, data); switch ( Requestcode) {case (Pick_contact): if (RESULTCODE == ACTIVITY.RESULT_OK) {uri contactdata = data.getdata (); Cursorloader cursorloader = new cursorloader (This, contactdata, null, null, null, null); Cursor cursor =cursorloader.loadinbackground (); if (Cursor.movetofirst ()) {string contactid = cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id)); String name = cursor.getstring (Cursor.getcolumnindexOrthrow (ContactsContract.Contacts.DISPLAY_NAME)); string phonenumber = "This contact has not entered a phone number"; Cursor phones = getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, contactscontract.commondatakinds.phone.contact_id + "=" +contactid, null, null); if ( Phones.movetofirst ()) {phonenumber = phones.getstring (Phones.getcolumnindex ( ContactsContract.CommonDataKinds.Phone.NUMBER));} Phones.close (); edittext show = (EditText) Findviewbyid (r.id.show); Show.settext (name); edittext phone = (EditText) Findviewbyid (r.id.phone);p hone.settext (PhoneNumber);} Cursor.close ();} Break;}}}
The value returned by ContactsContract.CommonDataKinds.Phone.CONTACT_ID is null
============ Solution 1============
Reference 3 floor BDMH reply:
Not all can get, because the mobile operator did not put the mobile phone number data written to the SIM card
Are you from the alien?
============ Solution 2============
Put
Intent.settype ("Vnd.android.cursor.item/phone");
Change into
Intent.settype ("Vnd.android.cursor.item/phone_v2");
Then remove the cursor that was created repeatedly.
The code is as follows:
Public class sysaction extends activity { final int PICK_CONTACT = 0; @Override protected void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( R.layout.activity_sys_action); Button bn = (Button) Findviewbyid (r.id.bn); bn.setonclicklistener (New onclicklistener () { @Override public void onclick (VIEW V) { // todo auto-generated method stub intent intent = new intent (); intent.setaction (Intent.ACTION_ Get_content); Intent.settype ("Vnd.android.cursor.item/phone_v2"); startactivityforresult (intent,pick_contact); } }); } @TargetApi (build.version_codes. Honeycomb) @SuppressLint ("Newapi") public void Onactivityresult (Int requestcode, int resultCode, intent data) { Super.onactivityresult (Requestcode, resultcode, data); switch (Requestcode) { case (pick_contact): if ( RESULTCODE == ACTIVITY.RESULT_OK) { uri contactdata = data.getdata (); cursorloader cursorloader = new cursorloader (This, contactdata, null, null, null, null); &nbsP; cursor cursor =cursorloader.loadinbackground (); if (Cursor.movetofirst ()) { string name = cursor.getstring (Cursor.getcolumnindexorthrow (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); string phonenumber = cursor.getstring (Cursor.getcolumnindex ( ContactsContract.CommonDataKinds.Phone.NUMBER)); EditText show = (EditText) Findviewbyid (r.id.show); show.settext (name); EditText phone = (EditText) Findviewbyid (r.id.phone); phone.settext (PhoneNumber) ; } cursor.close (); } break; } }}
============ Solution 3============
Such
/** Read Address Book async handler*/
Private Asyncqueryhandler asyncquery;
/**
* Read Contacts
*/
private void Readcontacts () {
Asyncquery = new Contactasyncqueryhandler (Getcontentresolver ());
Uri uri = uri.parse ("Content://com.android.contacts/data/phones");
string[] projection = {"_id", "Display_name", "Data1", "Sort_key"};
Asyncquery.startquery (0, NULL, URI, projection, null, NULL,
"Sort_key COLLATE localized ASC");
}
Querying contacts asynchronously
Private class Contactasyncqueryhandler extends Asyncqueryhandler {
Public Contactasyncqueryhandler (Contentresolver CR) {
Super (CR);
}
protected void Onquerycomplete (int token, Object cookie, cursor cursor) {
if (cursor! = NULL && cursor.getcount () > 0) {
arraylist<callsitem> list = new arraylist<callsitem> ();
Cursor.movetofirst ();
for (int i = 0; i < Cursor.getcount (); i++) {
Callsitem item = new Callsitem ();
Cursor.movetoposition (i);
String name = cursor.getstring (1);
String number = cursor.getstring (2);
String SortKey = cursor.getstring (3);
if (Number.startswith ("+86")) {
Number = number.substring (3); Remove +86
}
Item.name = name;
Item.number = number;
Item.sort = SortKey;
List.add (item);
System.out.println ("Name:" + name + "Number:" + number + "Sort:" + SortKey);
}
App.alllist = list;
}
}
}
Android get contact's name and phone number, unable to get phone