可能重复:
从 android 联系人选择器获取联系人信息
您好,我正在尝试从我的应用程序中的 android 联系人列表中获取联系人号码,其中我给出了一个按钮 onclick,我正在调用该按钮来调用联系人,我还在清单文件中添加了 read_contact 权限,但我得到了结果代码值 -1 ....wat 是错误的???任何人都可以帮助我...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friendinvite);
Button btnFriend=(Button)findViewById(R.id.btnFriend);
btnFriend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent contact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contact, 1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Cursor c = managedQuery(contactData,null,null,null,null);
String id=null;
String name=null;
String phone=null;
if(c.moveToFirst()){
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while(pCur.moveToNext()){
phone = c.getString(c.getColumnIndex(Phone.NUMBER));
}
}
}
}
}