TextView result = (TextView)findViewById(R.id.result); Intent intent = getIntent(); Bundle b = intent.getExtras(); String name = b.getString("name"); String[] projection = new String[]{ContactsContract.Contacts._ID,Phone.DISPLAY_NAME,Phone.NUMBER}; Uri contacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; String[] args = {name}; ContentResolver cr = this.getContentResolver(); Cursor cursor = cr.query(contacts, projection, Phone.DISPLAY_NAME+"=?", args, Phone.DISPLAY_NAME+" ASC"); if(cursor.moveToFirst()){ String name1 = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)); String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER)); result.setText(name1+":"+number); }
其中对于语句 Cursor cursor = cr.query(contacts, projection, Phone.DISPLAY_NAME+"=?", args, Phone.DISPLAY_NAME+" ASC");,在2.0以前第三个参数可以写为"name=?",经笔者测试后,在2.x中这种方法是行不通的,需改为文中的样子。