如何获取本地联系人帐户类型?

时间:2013-07-19 06:11:55

标签: android

我正在尝试获取本地联系人帐户类型。下面是它的代码,它可以在三星设备上正常工作,但不能在sony,micromax等上工作。请指出问题在哪里并解决此问题

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;

System.out.println(accountType);
System.out.println(accountName);

//preference.setString("contactAccountType", accountType);
//preference.setString("contactAccountName", accountName);

}

1 个答案:

答案 0 :(得分:0)

尝试这个,它适用于索尼,但不适用于三星。不知道为什么会这样。我也在研究同样的问题。

/**
 * Get All Account available in Phone and add into list
 */
private void getAllAccounts() {
    int counter = 0;
    final AccountManager accManager = AccountManager
            .get(Activity.this);
    final Account accounts[] = accManager.getAccounts();

    for (int i = 0; i < accounts.length; i++) {
        Log.i(TAG, "Name " + accounts[i].name + ", Type "
                    + accounts[i].type);
    }
}