【问题标题】:No phonenr from ContactsContractContactsContract 中没有电话号码
【发布时间】:2012-03-31 20:36:38
【问题描述】:

我在使用 ContactsContract 从联系人那里获取电话号码时遇到了一些问题 我的代码是

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
if (resultCode == RESULT_OK) {  
    switch (requestCode) {  
    case CONTACT_PICKER_RESULT:  
           Cursor cursor = null;
           String phone = "";
           try {
               Bundle extras = data.getExtras();
               Set<String> keys = extras.keySet();
               Iterator<String> iterate = keys.iterator();
               while (iterate.hasNext()) {
                   String key = iterate.next();
                   Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
               }

               Uri result = data.getData();
               Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

               // get the contact id from the Uri
               String id = result.getLastPathSegment();
               cursor = getContentResolver().query(Phone.CONTENT_URI,null, Phone.CONTACT_ID + "=?", new String[] { id },null);

               int PhoneIdx = cursor.getColumnIndex(Phone.NUMBER);


               if (cursor.moveToFirst()) {
                   phone = cursor.getString(PhoneIdx);
                   Log.v(DEBUG_TAG, "Got number: " + phone);

               } else {
                   Log.w(DEBUG_TAG, "No results");
               }

           } catch (Exception e) {
               Log.e(DEBUG_TAG, "Failed to get Number", e);

           } finally {
               if (cursor != null) {
                   cursor.close();
               }
               EditText ponenumber = (EditText) findViewById(R.id.editPhone1);
               ponenumber.setText(phone);

               if (phone.length() == 0) {
                   Toast.makeText(this, "Contact has no phone number",
                           Toast.LENGTH_LONG).show();
               }

           }
        break;  
    }  

} else {  
    Log.w(DEBUG_TAG, "Warning: activity result not ok");  
}  

}

每当我运行它并选择一个联系人时,我都会得到“联系人没有电话号码”。我不知道为什么,有人有什么想法吗?

问候

【问题讨论】:

  • 您的实际需求是什么,我想我可以帮助您
  • 获取列表中所选联系人的电话号码,但atm我得到“联系人没有电话号码”

标签: android android-contacts contactscontract phone-number


【解决方案1】:

您应该使用“电话选择器”意图,而不是“联系人选择器”:

static final int REQUEST_SELECT_PHONE_NUMBER = 1;

public void selectContact() {
    // Start an activity for the user to pick a phone number from contacts
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the phone number
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // If the cursor returned is valid, get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
}

取自以下文档: https://developer.android.com/guide/components/intents-common.html#Contacts

让用户从 联系人,例如电话号码、电子邮件地址或其他数据类型, 使用 ACTION_PICK 操作并将 MIME 类型指定为 下面列出的内容类型,例如 CommonDataKinds.Phone.CONTENT_TYPE 获取联系人的电话号码。

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 1970-01-01
    • 2011-06-11
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多