懒码农。。。。。。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
public void onCreate(Bundle savedInstanceState)
    {
        TextView tv = new TextView(this);
        String string = "";        
        super.onCreate(savedInstanceState);    
        //得到ContentResolver对象
        ContentResolver cr = getContentResolver();  
        //取得电话本中开始一项的光标
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, nullnullnullnull);
        while(cursor.moveToNext())
        { 
            try{
                int idColumn  = cursor.getColumnIndex(ContactsContract.Contacts._ID); 
                String contactId = cursor.getString(idColumn);   
                //取得联系人名字
                int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
                String contact = cursor.getString(nameFieldColumnIndex); 
                //取得电话号码
                String phoneNumber = ""; 
                //取得该联系人,电话数量
                int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));   
                if(phoneCount>0){   
                    //获得联系人的电话号码   
                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId,
                            nullnull);   
                    if(phones.moveToFirst()){
                        do{   
                            //遍历所有的电话号码   
                            phoneNumber += phones.getString(phones.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER)) + "\t";   
                            System.out.println(phoneNumber);   
                        }while(phones.moveToNext());   
                    }
                }
                string += contact + ":" + phoneNumber +"\n";
            }catch(Exception ex){
                ex.printStackTrace();
            }
            
            //cursor.moveToNext();
        }
        cursor.close();
        //设置TextView显示的内容
        tv.setText(string);
        //显示到屏幕
        setContentView(tv);
    }
posted on 2011-11-08 13:48  阿彬  阅读(315)  评论(0编辑  收藏  举报