【问题标题】:Obtaining phone number from lookup URI从查找 URI 获取电话号码
【发布时间】:2012-01-25 13:40:42
【问题描述】:

我一直在尝试使用他们的查找 URI 获取联系人的电话号码,但我无法正常工作。

Cursor myC = getContentResolver().query(lookupURI, null, null,
                        null, null);
                String phoneNumber;
                if (myC.moveToFirst()) {
                    while (myC.moveToNext()) {
                        phoneNumber = myC.getString(myC
                                .getColumnIndex(Phone.NUMBER));
                        Log.v("t", "phone number is: " + phoneNumber);
                    }
                }

lookupURI.toString() 是这个 URI:content://com.android.contacts/contacts/lookup/0r1-304846522C3052482C4A3442423C3248/1

谁知道我做错了什么?

【问题讨论】:

    标签: android uri android-contacts android-cursor


    【解决方案1】:

    不能保证这将适用于 4.0,因为我有一段时间没有使用它但在 2.3.3 上可以正常工作:

    为了获取contactId,我首先让用户选择一个联系人:

    public void clickSelectContact(View v) {
        Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(i, CONTACTS_REQUEST_CODE);
    }
    

    当用户选择了一个联系人时,它会回到这个方法:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CONTACTS_REQUEST_CODE){
            if(resultCode == RESULT_OK){
                 Uri uri = data.getData();
                 System.out.println("uri: "+uri);
                 System.out.println("PHONE NUMBER: " +  PhoneUtils.getContactPhoneNumber(this, uri.getLastPathSegment()));
            }
        }
    }
    

    调用我的静态 util 类:

    private static final String TAG = "PhoneUtils";
    
    public static String getContactPhoneNumber(Context context, String contactId) {
       int type = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
       String phoneNumber = null;
    
       String[] whereArgs = new String[] { String.valueOf(contactId), String.valueOf(type) };
    
       Log.d(TAG, "Got contact id: "+contactId);
    
       Cursor cursor = context.getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                null,
                                ContactsContract.CommonDataKinds.Phone._ID + " = ? and " + ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", 
                                whereArgs, 
                                null);
    
      int phoneNumberIndex = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
      if (cursor != null) {
           Log.d(TAG, "Returned contact count: "+cursor.getCount());
           try {
                 if (cursor.moveToFirst()) {
                     phoneNumber = cursor.getString(phoneNumberIndex);
                 }
                } finally {
                   cursor.close();
                }
       }
    
      Log.d(TAG, "Returning phone number: "+phoneNumber);
      return phoneNumber;
    }
    

    其中contactId = lookupURI.getLastPathSegment();

    这么简单的事情太复杂了! :-(

    附:您可能需要在清单中获得此权限:

     <uses-permission android:name="android.permission.READ_CONTACTS" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多