使用Intent.ACTION_PICK时是否可以排除SIM卡联系人?

Mak*_*iev 5 android android-intent android-contacts

我需要在我的应用中选择联系人,并希望排除存储在我的SIM卡中的联系人.有可能ACTION_PICK吗?

Kon*_*nov 1

不,这是不可能的

不幸的是,目前还不可能

为了证明这一点,让我们深入研究ContanctsListActivity.
这里有一个onCreate()方法Activity。其中,ContactApp 读取我们传递给它的Intent( ) 并分别处理它:ACTION_PICK

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mIconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
    mContactsPrefs = new ContactsPreferences(this);
    mPhotoLoader = new ContactPhotoLoader(this, R.drawable.ic_contact_list_picture);

    // Resolve the intent
    final Intent intent = getIntent();

    // Allow the title to be set to a custom String using an extra on the intent
    String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
    if (title != null) {
        setTitle(title);
    }

    String action = intent.getAction();
    String component = intent.getComponent().getClassName();

    // When we get a FILTER_CONTACTS_ACTION, it represents search in the context
    // of some other action. Let's retrieve the original action to provide proper
    // context for the search queries.
    if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
        mSearchMode = true;
        mShowSearchSnippets = true;
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mInitialFilter = extras.getString(UI.FILTER_TEXT_EXTRA_KEY);
            String originalAction =
                    extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
            if (originalAction != null) {
                action = originalAction;
            }
            String originalComponent =
                    extras.getString(ContactsSearchManager.ORIGINAL_COMPONENT_EXTRA_KEY);
            if (originalComponent != null) {
                component = originalComponent;
            }
        } else {
            mInitialFilter = null;
        }
    }

    Log.i(TAG, "Called with action: " + action);
    mMode = MODE_UNKNOWN;
    if (UI.LIST_DEFAULT.equals(action) || UI.FILTER_CONTACTS_ACTION.equals(action)) {
        ..... 
        else if (Intent.ACTION_PICK.equals(action)) {
        // XXX These should be showing the data from the URI given in
        // the Intent.
        final String type = intent.resolveType(this);
        if (Contacts.CONTENT_TYPE.equals(type)) {
            mMode = MODE_PICK_CONTACT;
        } else if (People.CONTENT_TYPE.equals(type)) {
            mMode = MODE_LEGACY_PICK_PERSON;
        } else if (Phone.CONTENT_TYPE.equals(type)) {
            mMode = MODE_PICK_PHONE;
        } else if (Phones.CONTENT_TYPE.equals(type)) {
            mMode = MODE_LEGACY_PICK_PHONE;
        } else if (StructuredPostal.CONTENT_TYPE.equals(type)) {
            mMode = MODE_PICK_POSTAL;
        } else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(type)) {
            mMode = MODE_LEGACY_PICK_POSTAL;
        }
       ....
       // VERY LONG IF WITH DIFFERENT MODE-SELECTION
       ....
    }
    .....
    if (mMode == MODE_JOIN_CONTACT) {
        setContentView(R.layout.contacts_list_content_join);
    } else if (mSearchMode) {
        setContentView(R.layout.contacts_search_content);
    } else if (mSearchResultsMode) {
        setContentView(R.layout.contacts_list_search_results);
    } else {
        setContentView(R.layout.contacts_list_content);
    }

    setupListView();
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是一个很长的方法(我也建议检查setupListView()方法),但非常简单。而且,正如您所看到的,您无法传递任何参数来指定您要从中选择的联系人来源。您在这里唯一可以配置的是mMode要使用的特定 ContactsApp(MODE_PICK_CONTACTMODE_PICK_PHONE等) - 但不幸的是,可能的模式数量非常有限,只有 6 种,而且没有一个适合您。

(如果有人需要传递mModeContanctsListActivity- 使用intent的setType()方法,例如intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);:)

解决方法

作为一种解决方法 - 正如微小的阳光在评论中建议的那样 - 在应用程序中呈现非 SIM 联系人,然后从那里选择您需要的联系人。
如何获取所有 Android 联系人,但不包含 SIM 卡上的联系人- 此链接看起来是最有前途的链接,解释了如何查询除 SIM 联系人之外的所有联系人的光标。

我希望,它有帮助