android 进入通讯录

 

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("vnd.android.cursor.item/phone");
startActivityForResult(intent, 2);

 

//在onActivityResult里面:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode==RESULT_OK) {
            switch (requestCode) {
              case 2:

                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                c.moveToFirst();
                //String id= c.getString(c.getColumnIndex(Contacts._ID));   //在这取到的id 和原先的id不一样,不知道是怎么回事所也下面就用name 来获得电话号码
    
                String name = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME));  //取得通讯录中选中的名称

                String phoneNum=getPhoneNum(name);   // 取得所选的电话号码

                System.out.println("name "+name +" number "+phoneNum);
                break;
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
 
private String getPhoneNum(String name) {
        Cursor phones= managedQuery(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME+" = '"+name+"'", null, null);
        phones.moveToFirst();
        String phoneNum = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        return phoneNum;
    }