【问题标题】:Android - back from Contacts intent without selecting oneAndroid - 从联系人意图返回而不选择一个
【发布时间】:2016-08-28 18:03:46
【问题描述】:

在我的 android 应用程序中,我有一个人员列表(姓名和电话号码),我让用户通过从他们的联系人列表中导入一个人来将一个人添加到应用程序的数据库中。一切正常,但用户可以单击按钮导入一个新的人,然后返回,因此联系人意图将在没有实际选择联系人的情况下关闭。

所以我需要知道在我的代码中放置 if 或其他内容的位置,以便查看用户是否选择了联系人。

private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();

            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};

            Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
            cursor.moveToFirst();

            // Retrieve the name from the DISPLAY_NAME column
            String name =  cursor.getString(cursor.getColumnIndex(ontactsContract.Contacts.DISPLAY_NAME));

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(column);

            // update database
            dbManager.addPerson(name, number, true);
        }
    }

}

那么我如何检查用户是否真的选择了联系人或返回?

【问题讨论】:

    标签: android android-intent android-contacts back-button


    【解决方案1】:

    所以我需要知道在我的代码中放置 if 或其他内容的位置,以便查看用户是否选择了联系人。

    您已经拥有此代码:if (resultCode == RESULT_OK)。如果用户按 BACK,您将不会得到RESULT_OK

    【讨论】:

      【解决方案2】:

      您正在使用 startActivityForResult,一旦您的

      Intent.ACTION_PICK, Uri.parse("content://contacts")
      

      jobs 结束了,因为用户已经选择了一个联系人。

      为了获得所选联系人的姓名、号码或任何其他详细信息,您可以在 onActivityResult 回调中执行如下操作:

      @Override
      public void onActivityResult(int reqCode, int resultCode, Intent data) {
          super.onActivityResult(reqCode, resultCode, data);
      
          if(reqCode == PICK_CONTACT_REQUEST) {
                  String cNumber = null;
                  String cName = null;
      
                  if (resultCode == Activity.RESULT_OK) {
      
                      Uri contactData = data.getData();
                      Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                      String hasPhone = null;
                      String contactId = null;
      
                      if (cursor != null) {
                          cursor.moveToFirst();
                          try {
                              hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                              contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                          } catch (IllegalArgumentException e) {
                              Log.e(TAG, "Exception Message : " + e.getMessage());
                              DisplayMessage.error("Sorry can't access contacts. Check permissions settings.", this);
                              return;
                          }
      
                          if (hasPhone != null && hasPhone.equals("1")) {
                              Cursor phones = getContentResolver().query
                                      (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                      + " = " + contactId, null, null);
                              if (phones != null) {
                                  while (phones.moveToNext()) {
                                      String phoneNumber = phones.getString(phones.getColumnIndex
                                              (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                                      cNumber = phoneNumber;
                                  }
                                  phones.close();
                              }
      
                              cName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                              Log.d(TAG, "Contact name is:" + cName);
                          }
                          cursor.close();
                      }
                  }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-10
        • 2013-12-22
        • 2015-01-22
        • 2019-03-31
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 2021-09-18
        相关资源
        最近更新 更多