13

我想从仅属于电话/SIM 卡的 ContactsContract API 读取联系人,并希望避免从 facebook 和 gmail 等其他应用程序同步联系人。我在模拟器上测试了我的代码,它工作正常,但在真实设备上它不返回任何结果。

ContentResolver cr = AndroidContext.getContext()
                    .getContentResolver();
Cursor nativeContacts = cr.query(RawContacts.CONTENT_URI,
                    new String[] { RawContacts._ID, RawContacts.VERSION, RawContacts.CONTACT_ID },
                    RawContacts.DELETED + "<> 1 AND " + RawContacts.CONTACT_ID
                            + " IS NOT NULL AND " + RawContacts.ACCOUNT_NAME + " IS NULL AND "
                            + RawContacts.ACCOUNT_TYPE + " IS NULL", null, null);

我猜在设备上默认帐户的帐户类型和名称不为空,解决方案是什么?

4

3 回答 3

10

我还没有找到获取 SIM 卡帐户的方法。但我使用下面的代码来获取默认帐户名称和类型。

public void getDefaultAccountNameAndType() {
    String accountType = "";
    String accountName = "";

    long rawContactId = 0;
    Uri rawContactUri = null;
    ContentProviderResult[] results = null;

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNT_NAME, null).withValue(RawContacts.ACCOUNT_TYPE, null).build());

    try {
        results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        ops.clear();
    }

    for (ContentProviderResult result : results) {
        rawContactUri = result.uri;
        rawContactId = ContentUris.parseId(rawContactUri);
    }

    Cursor c = getContentResolver().query(
            RawContacts.CONTENT_URI
            , new String[] {RawContacts.ACCOUNT_TYPE, RawContacts.ACCOUNT_NAME}
            , RawContacts._ID+"=?"
            , new String[] {String.valueOf(rawContactId)}
            , null);

    if(c.moveToFirst()) {
        if(!c.isAfterLast()) {
            accountType = c.getString(c.getColumnIndex(RawContacts.ACCOUNT_TYPE));
            accountName = c.getString(c.getColumnIndex(RawContacts.ACCOUNT_NAME));
        }
    }

    getContentResolver().delete(rawContactUri, null, null);

    c.close();
    c = null;

    preference.setString("contactAccountType", accountType);
    preference.setString("contactAccountName", accountName);
}
于 2011-05-23T11:24:46.703 回答
3

我和你有同样的经历,只能建议两种解决方法:

  1. 让用户做决定。例如,向他展示所有原始联系人的列表,然后让他选择哪个是电话簿联系人还是 SIM 卡联系人。

  2. 我对三种不同设备的体验是 AccountManager 不知道用于存储这些电话联系人的帐户。例如,当您像 ( AccountManager.getAccounts()) 一样从 AccountManager 获取帐户数组时,“com.htc.android.pcsc”不在列表中!但是您可以完全利用这一事实:排除所有已知的帐户类型/名称,您获得的列表应该是所有电话簿联系人/simcontacts 的列表。

希望这些想法对您有所帮助:) 我想阅读您对这些解决方法的想法,最终我错过了一些东西或者有更好的解决方法。

于 2011-02-20T10:04:45.817 回答
1

我解决了这个问题。

Account[] accountList = AccountManager.get(this).getAccounts();

String accountSelection = "";
for(int i = 0 ; i < accountList.length ; i++) {
  if(accountSelection.length() != 0)
    accountSelection = accountSelection + " AND ";
  accountSelection = accountSelection + ContactsContract.Groups.ACCOUNT_TYPE + " != '" +  accountList[i].type + "'";
}
于 2011-04-27T06:33:22.643 回答