Android get contact's name and phone number, unable to get phone

Source: Internet
Author: User



============ 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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");
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 yet";
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);
phone.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);
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.