在Android 2.2上阅读联系人

在Android 2.2上阅读联系人,android,android-contacts,phone-number,contactscontract,Android,Android Contacts,Phone Number,Contactscontract,我正在使用以下代码读取Android 2.2上的电话联系人姓名和号码。 我想做的是读取每个联系人的姓名和号码,并将其附加到Stringbuilder 目前我可以获取联系人姓名,但获取联系人号码时遇到一些问题。 我怎样才能做到这一点?请提供一个示例片段以获取数字 public class main extends Activity { /** Called when the activity is first created. */ public static StringBuilder s =

我正在使用以下代码读取Android 2.2上的电话联系人姓名和号码。
我想做的是读取每个联系人的姓名和号码,并将其附加到
Stringbuilder

目前我可以获取联系人姓名,但获取联系人号码时遇到一些问题。
我怎样才能做到这一点?请提供一个示例片段以获取数字

public class main extends Activity {
/** Called when the activity is first created. */
public static StringBuilder s = new StringBuilder();
public static String number= new String();// = new String();
public static String contact = new String();
public final String phonenumber = new String();
public String formattedPhoneNumber = new String();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub  
 //-----------code to run when button is clicked-------------------
    EditText textfield = (EditText) findViewById(R.id.textfield);

    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, null,null, null);
    if(cursor != null) {
        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
            //Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
        }
        cursor.close();

    while(people.moveToNext()) {
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
       contact = people.getString(nameFieldColumnIndex);
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
      //umber = people.getString(numberFieldColumnIndex);
    }

    people.close();


    //--------------code for getting phone number---

    textfield.setText("hello: "+contact+" "+ formattedPhoneNumber);


    //+String.valueOf(number));


    //---------------end of code-----------------------------



}


};});}}
抄袭自:


你能告诉我我应该在这里写什么/用手机号码做点什么吗???因为我试着看完了教程,但没办法让它正常工作。字符串号码应该包含电话号码。您可以将其附加到StringBuilder。
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    "DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
    String contactId =
        cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    //
    //  Get all phone numbers.
    //
    Cursor phones = cr.query(Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
            case Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
            }
    }
    phones.close();
   }
   cursor.close();