如何在Android中识别本地电话联系人和SYNC电话联系人? [英] How to identify a local phone contact and an SYNC phone contact in android?

查看:95
本文介绍了如何在Android中识别本地电话联系人和SYNC电话联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我找到以下问题的解决方案吗。

Can anybody guide me to find the solution for the following problem.


  1. 我必须确定电话联系人是否保存在本地或从电子邮件?(以编程方式)

我从 Google文档表示 ContactsContract.Groups,其中包含有关原始联系人组(例如Gmail联系人组)的信息。当前的API不支持跨多个帐户的组的概念。

基于此,我尝试了以下代码。

Based on that i have tried the following code.

 StringBuffer output = new StringBuffer();
   final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, 
            ContactsContract.Groups.TITLE,
            ContactsContract.Groups.SUMMARY_WITH_PHONES
            };

Cursor c =  getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
        null, ContactsContract.Groups.TITLE);

  int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID);
  int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE);
  output.append("title"+IDX_TITLE+"\n");
    Map<String,GroupInfo> m = new HashMap<String, GroupInfo>();

while (c.moveToNext()) {
    output.append("test...\n");
    GroupInfo g = new GroupInfo();
    g.id = c.getString(IDX_ID);
    g.title = c.getString(IDX_TITLE);
    output.append("title"+c.getString(IDX_TITLE)+"\n");
    int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES));
    if (users>0) {
        // group with duplicate name?
        GroupInfo g2 = m.get(g.title);
        if (g2==null) {
            m.put(g.title, g);
            output.append("title"+g.title+"\n");
            groups.add(g);
        } else {
            g2.id+=","+g.id;
        }
    }
}
outputText.setText(output);
c.close();

但没有希望。

推荐答案

我正在发布此答案供将来使用。我们可以使用 RawContacts.SOURCE_ID

I am posting this answer for future use. We can differentiate the local phone contacts and the sync contacts by using the field called RawContacts.SOURCE_ID

来区分本地电话联系人和同步联系人。 此处

It is described here

SOURCE_ID

读/写

唯一标识此行为其源帐户的字符串。通常,它是在插入原始触点时设置的,此后再也不会更改。一个值得注意的例外是一个新的原始联系人:它将具有一个帐户名和类型(可能还有一个数据集),但是没有源ID。这向同步适配器指示需要在服务器端创建一个新联系人,并将其ID存储在电话上的相应SOURCE_ID字段中。

SOURCE_ID
read/write
String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone.

示例代码如下,它给出了同步联系人的ID,而其他联系人则为null。

The sample code is follows, it gives the id for sync contacts and null for others.

private void testContact() {
        StringBuffer output = new StringBuffer();
        ContentResolver resolver = getContentResolver();
        Cursor contacts = resolver.query(Contacts.CONTENT_URI, null,
                Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID
                        + " ASC");
        Cursor data = resolver.query(Data.CONTENT_URI, null, Data.MIMETYPE
                + "=? OR " + Data.MIMETYPE + "=?", new String[]{
                Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
                Data.CONTACT_ID + " ASC");

        int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
        int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
        int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
        int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
        boolean hasData = data.moveToNext();

        while (contacts.moveToNext()) {
            long id = contacts.getLong(idIndex);
            Uri rawContactUri = 
                      ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);

                    Uri entityUri =
                      Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

                    Cursor c =
                      getContentResolver().query(
                        entityUri,
                        new String[] {
                                  RawContacts.ACCOUNT_NAME,
                          RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
                        null, null, null);

                    try {
                         while (c.moveToNext()) {
                             String sourceId = c.getString(0);
                             if (!c.isNull(1)) {

                                 String source_id = c.getString(1);
                             try {
                                     output.append(c.getString(4)+sourceId+" "+source_id+"\n");

                                     //output.append(datas+ "Sync1 "+  c.getString(4)+" Sync2 "+  c.getString(5)+" Sync3"+  c.getString(6)+" Sync4 "+  c.getString(7)+"\n");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                 //decide here based on mimeType, see comment later
                             }
                         }
                    } finally {
                         c.close();
                    }

        }

        outputText.setText(output);
    }

这篇关于如何在Android中识别本地电话联系人和SYNC电话联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆