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

Is it possible to exclude SIM contacts when using Intent.ACTION_PICK?

我需要在我的应用程序中选择联系人,并希望排除存储在我的 SIM 卡中的联系人。 ACTION_PICK 可以吗?

不,不可能

不幸的是,目前不可能

为了证明这一点,让我们深入研究ContanctsListActivity的源代码。
Here's ActivityonCreate() 方法。在其中,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();
    ...
}

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

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

解决方法

作为一种解决方法 - 正如 tiny sunlight 在评论中所建议的那样 - 在应用程序中呈现非 SIM 卡联系人并从中选择你需要的联系人。
How to get all android contacts but without those which are on SIM - 这个 link 看起来最有前途,解释了如何查询所有联系人的光标,SIM 卡除外。

希望对你有所帮助