您好我想从联系人列表中获取一个电话号码。我找到的代码可以获取整个联系人列表的电话号码,我想要的只是点击项目的电话号码。任何帮助将不胜感激。谢谢..
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",new String[] { id }, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getActivity(), phoneNo,Toast.LENGTH_SHORT).show();
}
}
}
}
}
});
答案 0 :(得分:2)
当您在联系人列表中并单击联系人时,您将获得Intent.ACTION_PICK
意图的结果。
因此,要处理结果,您必须覆盖onActivityResult()
方法。
您会在Intent
返回的数据onActivityResult()
对象中收到联系人的查找URI。
所以,你所要做的就是:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CONTACT_PICKER_RESULT){
if(resultCode==Activity.RESULT_OK){
Uri uri = data.getData();
ContentResolver contentResolver = getContentResolver();
Cursor contentCursor = contentResolver.query(uri, null, null,null, null);
if(contentCursor.moveToFirst()){
String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =
contentCursor.getString(contentCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
phones.moveToFirst();
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phoneNUmber", "The phone number is "+ contactNumber);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
然后你只需要用contactNumber
上的值做任何你想做的事。
希望能帮助你。
答案 1 :(得分:0)
首先在AndroidManifest.xml中添加此行以获取用户的权限。
移动用户还需要允许此权限。可以通过设置手动完成
<uses-permission android:name="android.permission.READ_CONTACTS"/>
现在在按钮中单击侦听器编写此代码。
contactsButtton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,1);
}
});
现在来自onCreate()函数的外部。你必须覆盖onActivityResult()函数。 与此相似
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1 :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(contactData, null, null, null, null);
if (cur.getCount() > 0) {// thats mean some resutl has been found
if(cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("Names", name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Number", phoneNumber);
}
phones.close();
}
}
}
cur.close();
}
break;
}
}