首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android内容解析器返回在移动电话号码中激活的所有帐户

Android内容解析器返回在移动电话号码中激活的所有帐户
EN

Stack Overflow用户
提问于 2015-11-06 17:23:06
回答 1查看 324关注 0票数 0

当试图使用下面的代码从android检索联系人列表时,它会返回多个帐户,如google/gmail、whatsapp和soma (激活的帐户数量取决于联系人副本的数量!)。请帮我把它移开。

代码语言:javascript
复制
 cursor1=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI ,null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" ASC");              
 startManagingCursor(cursor1);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID,}; 
  int[] to = {android.R.id.text1, android.R.id.text2}; 
listadapter = new SimpleCursorAdapter(getBaseContext(), android.R.layout.simple_list_item_2, cursor1, from, to);        
 setListAdapter(listadapter);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-06 17:29:17

Simpy尝试下面的代码,它有更多的验证,并检查它是否有电话号码。但是你的安多尼德装置的联系应该安排得很好。

代码语言:javascript
复制
   private void displayContacts() {

      ContentResolver cr = 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(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                     }
                    pCur.close();
                }
            }
        }
    }

您还可以在这里获得一些帐户过滤,比如使用下面的代码从应用程序帐户中过滤掉,从电话联系人中获取应用程序联系人:

代码语言:javascript
复制
Cursor c = getContentResolver().query(
        RawContacts.CONTENT_URI,
        new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
        RawContacts.ACCOUNT_TYPE + "= ?",
        new String[] { "com.whatsapp" },
        null);

ArrayList<String> myWhatsappContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
    // You can also read RawContacts.CONTACT_ID to read the
    // ContactsContract.Contacts table or any of the other related ones.
    myWhatsappContacts.add(c.getString(contactNameColumn));
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33572251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档