从用户手机获取联系人
问题描述:
我正在开发一个使用firebase(用于后端)的简单信使应用程序。我希望将用户的联系电话号码与存储在我的数据库中的联系人电话号码进行比较,以便用户可以查看精致的联系人列表。我在谷歌查找,但我找不到一个方法来获取数字存储在一个单独的变量。我想下面的代码在堆栈溢出另一篇文章,但我在getactivity得到一个错误说如果你正在使用的代码中的活动我觉得比getActivity()不是“无法解析法”从用户手机获取联系人
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()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Log.e(name + "(mobile number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Log.e(name + "(home number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Log.e(name + "(work number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
Log.e(name + "(other number)", phoneNumber);
break;
default:
break;
}
}
pCur.close();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答
最有可能需要的话,你可以直接调用getContentResolver(),但如果仍然不行,试试这个来获取联系人和其他细节。我从我的项目之一复制代码,所以希望你能明白,
ArrayList<DeviceContacts> contactsList = new ArrayList<DeviceContacts>();
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// read id
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
/** read names **/
displayName = null;
displayName = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/** read image uri's **/
imageuri = null;
imageuri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
/** Phone Numbers **/
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
number = null;
typeStr = null;
while (pCur.moveToNext()) {
number = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
typeStr = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.i("ContactsList", "Contact number :" + number);
Log.i("ContactsList", "Number type :" + typeStr);
}
pCur.close();
/** EMAIL **/
Cursor emailCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
email = null;
while (emailCur.moveToNext()) {
email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
DeviceContacts dContacts = new DeviceContacts(); // simple model class
dContacts.setDisplayName(displayName);
dContacts.setmNumber(number);
dContacts.setEmail(email);
dContacts.setImageUri(imageuri);
contactsList.add(dContacts);
emailCur.close();
}
}
cur.close();
希望这可以帮助
感谢您对穆克什答复...我会尝试一下代码,我会给你我的反馈:) – ananymous59 2014-11-25 13:14:13
继续,并取得成功:) – 2014-11-25 13:19:54
感谢百万工作:) – ananymous59 2014-11-25 13:51:00