如何在android中获取联系号码?

时间:2012-01-15 08:40:41

标签: android contacts

我正在尝试在我的应用程序中获取联系人号码,但它只显示了ID和显示名称。任何人都可以纠正吗?我不知道如何获取联系号码。我只是想提取联系号码而不是添加联系号码。

package com.data;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public final class ContactManager extends Activity
{

public static final String TAG = "ContactManager";

private Button mAddAccountButton;
private ListView mContactList;
private boolean mShowInvisible;
private CheckBox mShowInvisibleControl;

/**
 * Called when the activity is first created. Responsible for initializing the UI.
 */
@Override
public void onCreate(Bundle savedInstanceState)
{
    Log.v(TAG, "Activity State: onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_manager);

    // Obtain handles to UI objects
//    mAddAccountButton = (Button) findViewById(R.id.addContactButton);
    mContactList = (ListView) findViewById(R.id.contactList);
    mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);

    // Initialize class properties
    mShowInvisible = false;
    mShowInvisibleControl.setChecked(mShowInvisible);

    // Register handler for UI elements

    mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
            mShowInvisible = isChecked;
            populateContactList();
        }
    });

    // Populate the contact list
    populateContactList();
}



private void populateContactList() {

    Cursor cursor = getContacts();
   String[] pno=new String[cursor.getCount()];
      if(cursor.getCount()>0)
      {
        int i=0;
        if(cursor.moveToFirst())
        {
            do
            {
        pno[i++]=(String)cursor.getString(0).toString() +"  "+       (String)cursor.getString(1);

            }while(cursor.moveToNext());
        }

    }

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(ContactManager.this,android.R.layout.simple_list_item_1,pno);
    mContactList.setAdapter(adapter);


}


private Cursor getContacts()
{
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (mShowInvisible ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}

}

3 个答案:

答案 0 :(得分:1)

修改您的查询projection以包含ContactsContract.Contacts.HAS_PHONE_NUMBER列。

然后,在do-while循环中,你可以这样做:

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(hasPhone) > 0) {
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
    String phoneNumber;
    while (cursor.moveToNext()) { //iterate over all contact phone numbers
        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    phones.close();
}

答案 1 :(得分:0)

您需要使用其他内容提供商uri。我将向您指出我的一个答案,以便我不会在stackoverflow中重复自己:https://stackoverflow.com/a/8646827/1108032。在这个答案中,我从电话号码中取出了名字,但我真的相信你可以轻易地恢复它。

答案 2 :(得分:0)

您应该使用Phone Uri或新的PhoneLookUp Uri获取显示名称和所有带有联系人ID的电话号码。

http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html