コード例 #1
0
  @Override
  public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
      case (PICK_CONTACT):
        if (resultCode == Activity.RESULT_OK) {
          Uri contactData = data.getData();
          CursorLoader loader = new CursorLoader(this, contactData, null, null, null, null);
          loader.registerListener(
              LOADER_ID_CONTACT,
              new Loader.OnLoadCompleteListener<Cursor>() {
                @Override
                public void onLoadComplete(final Loader<Cursor> loader, final Cursor data) {
                  if (data.moveToFirst()) {
                    int nameColumnIndex =
                        data.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
                    String name = data.getString(nameColumnIndex);
                    txtContacts.setText(name);
                  }
                }
              });
          loader.startLoading();
        }
        break;
    }
  }
コード例 #2
0
  /**
   * Initiates the interaction. This may result in a phone call or sms message started or a
   * disambiguation dialog to determine which phone number should be used.
   */
  @VisibleForTesting
  /* package */ void startInteraction(Uri uri) {
    if (mLoader != null) {
      mLoader.reset();
    }

    final Uri queryUri;
    final String inputUriAsString = uri.toString();
    if (inputUriAsString.startsWith(Contacts.CONTENT_URI.toString())) {
      if (!inputUriAsString.endsWith(Contacts.Data.CONTENT_DIRECTORY)) {
        queryUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY);
      } else {
        queryUri = uri;
      }
    } else if (inputUriAsString.startsWith(Data.CONTENT_URI.toString())) {
      queryUri = uri;
    } else {
      throw new UnsupportedOperationException(
          "Input Uri must be contact Uri or data Uri (input: \"" + uri + "\")");
    }

    mLoader =
        new CursorLoader(
            this, queryUri, PHONE_NUMBER_PROJECTION, PHONE_NUMBER_SELECTION, null, null);
    mLoader.registerListener(0, this);
    mLoader.startLoading();
  }
コード例 #3
0
  public static void loadPhoneContacts(
      Context context,
      final List<Bundle> phoneContacts,
      final OnPhoneContactsLoadedListener listener) {
    final String[] PROJECTION =
        new String[] {
          ContactsContract.Data._ID,
          ContactsContract.Data.DISPLAY_NAME,
          ContactsContract.Data.PHOTO_URI,
          ContactsContract.Data.LOOKUP_KEY,
          ContactsContract.CommonDataKinds.Im.DATA
        };

    final String SELECTION =
        "("
            + ContactsContract.Data.MIMETYPE
            + "=\""
            + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
            + "\") AND ("
            + ContactsContract.CommonDataKinds.Im.PROTOCOL
            + "=\""
            + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
            + "\")";

    CursorLoader mCursorLoader =
        new CursorLoader(
            context, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, null);
    mCursorLoader.registerListener(
        0,
        new OnLoadCompleteListener<Cursor>() {

          @Override
          public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
            if (cursor == null) {
              return;
            }
            while (cursor.moveToNext()) {
              Bundle contact = new Bundle();
              contact.putInt(
                  "phoneid", cursor.getInt(cursor.getColumnIndex(ContactsContract.Data._ID)));
              contact.putString(
                  "displayname",
                  cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
              contact.putString(
                  "photouri",
                  cursor.getString(cursor.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
              contact.putString(
                  "lookup",
                  cursor.getString(cursor.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

              contact.putString(
                  "jid",
                  cursor.getString(
                      cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
              phoneContacts.add(contact);
            }
            if (listener != null) {
              listener.onPhoneContactsLoaded(phoneContacts);
            }
            cursor.close();
          }
        });
    try {
      mCursorLoader.startLoading();
    } catch (RejectedExecutionException e) {
      if (listener != null) {
        listener.onPhoneContactsLoaded(phoneContacts);
      }
    }
  }