Android 获取具有查找URI的联系人的所有电话号码
我无法获取包含所有联系人电话号码的工作光标:Android 获取具有查找URI的联系人的所有电话号码,android,android-contacts,android-cursor,Android,Android Contacts,Android Cursor,我无法获取包含所有联系人电话号码的工作光标: String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER}; Cursor phones = localContentResolver.query(lookupUri, projection, null, null, null); 我使用以下方法获取lookupUri: public static Uri getLookup
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor phones = localContentResolver.query(lookupUri, projection, null, null, null);
我使用以下方法获取lookupUri:
public static Uri getLookupUri (ContentResolver mContentResolver, String number) {
Uri uri=null;
Cursor contactLookupCursor =
mContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number)),
new String[] {PhoneLookup._ID, PhoneLookup.LOOKUP_KEY},
null,
null,
null);
if (contactLookupCursor.moveToNext()) {
uri=Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.LOOKUP_KEY)));
}
contactLookupCursor.close();
return uri;
}
然后我想得到一个包含所有联系人电话号码的光标:
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor phones = localContentResolver.query(lookupUri, projection, null, null, null);
但我只得到IllegalArgumentException:无效列数据1您试图从联系人行查询电话号码。联系人行没有电话号码。ContactsContract.CommonDataTypes.Phone.NUMBER的值为data1,ContactsContract.Contacts表中没有data1列 要查找联系人的所有电话号码,请执行以下操作: 找到你想要的联系人。您的getLookupUri很适合这样做,但您可能应该使用CONTENT\u VCARD\u URI调用withAppendedPath。请参阅Javadoc
要获取联系人的所有电话号码,请使用Contacts.Entity表。您正在尝试从Contacts行查询电话号码。联系人行没有电话号码。ContactsContract.CommonDataTypes.Phone.NUMBER的值为data1,ContactsContract.Contacts表中没有data1列 要查找联系人的所有电话号码,请执行以下操作: 找到你想要的联系人。您的getLookupUri很适合这样做,但您可能应该使用CONTENT\u VCARD\u URI调用withAppendedPath。请参阅Javadoc
要获取联系人的所有电话号码,请使用Contacts.Entity表。以下代码将获取android设备中的所有联系人
public class GetContactFromLookup extends Activity {
int CONTACTS_REQUEST_CODE =1;
static ContentResolver cr;
Activity thisActivity;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.test);
thisActivity = this;
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1 :
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
List<String> items = uri.getPathSegments();
String lookup_key = items.get(2);
Log.v("", "id-->"+items.get(2));
Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI;
Cursor mqCur = managedQuery(myContacts, null, null, null, null);
if(mqCur.moveToFirst())
{
do
{
String contact_id = mqCur.getString(mqCur
.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));
if(lookup_key.equals(contact_id)){
String phone_no = mqCur.getString(mqCur
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("", "phone_no-->"+phone_no);
}
}
while(mqCur.moveToNext());
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
我想这会对您有所帮助。以下代码将获取android设备中的所有联系人
public class GetContactFromLookup extends Activity {
int CONTACTS_REQUEST_CODE =1;
static ContentResolver cr;
Activity thisActivity;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.test);
thisActivity = this;
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1 :
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
List<String> items = uri.getPathSegments();
String lookup_key = items.get(2);
Log.v("", "id-->"+items.get(2));
Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI;
Cursor mqCur = managedQuery(myContacts, null, null, null, null);
if(mqCur.moveToFirst())
{
do
{
String contact_id = mqCur.getString(mqCur
.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));
if(lookup_key.equals(contact_id)){
String phone_no = mqCur.getString(mqCur
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("", "phone_no-->"+phone_no);
}
}
while(mqCur.moveToNext());
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
我想这会对您有所帮助。要使用联系人姓名获取电话号码,请使用此代码
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
我做了测试,结果很好
我从这个答案中得到它
要使用联系人姓名获取电话号码,请使用此代码
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
我做了测试,结果很好
我从这个答案中得到它
我不想获取所有联系人,但要获取已具有lookupUriOk的特定联系人的所有电话号码,请参见此。这将对您有所帮助。该示例仅从lookupUri提取contactId。我试图摆脱contactId,因为正如Android参考所说:假设你的应用程序存储了一个长的联系人ID。然后用户去手动将联系人与其他联系人合并。现在只有一个联系人,而以前只有两个联系人,而存储的长联系人ID不指向任何地方。在这种情况下,lookup键有助于解析联系人。我不想获取所有联系人,但要检索已具有lookupUriOk的特定联系人的所有电话号码,请参见此。这将对您有所帮助。该示例仅从lookupUri提取contactId。我试图摆脱contactId,因为正如Android参考所说:假设你的应用程序存储了一个长的联系人ID。然后用户去手动将联系人与其他联系人合并。现在只有一个联系人,而以前只有两个联系人,而存储的长联系人ID不指向任何地方。在这种情况下,查找键有助于解析联系人。