`
huangro
  • 浏览: 327475 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

通过ContactsContract类获取电话号码的改变

 
阅读更多
在2.0版本之前,获取通讯录中的联系人及其电话号码的方式如下:
        String string = "";
        super.onCreate(savedInstanceState);
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
        	int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
        	String contact = cursor.getString(nameFieldColumnIndex);
        	int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
        	String number = cursor.getString(numberFieldColumnIndex);
        	string += (contact+":"+number+"\n");
        }

但是在2.0及之后的版本中,获取联系人的方式不变,但是电话号码的方式发生改变, 具体代码如下:
        String string = "";
        super.onCreate(savedInstanceState);
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
        	int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
        	String contact = cursor.getString(nameFieldColumnIndex);
        	string += contact + ":";
        	String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        	Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
        			ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
        	while (phone.moveToNext()) 
        	{
        		int numberFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        		String strPhoneNumber = phone.getString(numberFieldColumnIndex);
        		string += " " + strPhoneNumber;
        	}
        	string += "\n";
        	phone.close();
        }
        cursor.close();


可见,在2.0之后的版本中要获取手机号码就只能通过先取得联系人ID然后再取得联系人电话号码, 而且号码可能不止一个.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics