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

时间:2016-01-25 12:07:18

标签: android android-intent android-contacts

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

1 个答案:

答案 0 :(得分:1)

不,它不可能

不幸的是,它现在可能

为了证明这一点,让我们深入研究 ContanctsListActivity 的源代码。
onCreate() Activity的{​​{1}}方法。在其中,ContactApp读取我们传递给它的IntentACTION_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();
    ...
}

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

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

解决方法

作为一种解决方法 - 在评论中建议微小阳光 - 在应用程序中渲染非SIM联系人并从那里挑选所需的联系人。
setupListView() - 此链接看起来最有希望解释如何使用除SIM之外的所有联系人查询游标。

我希望,这有帮助