【问题标题】:Android Contacts Content Provider sometimes returns empty phone numbersAndroid Contacts Content Provider 有时会返回空电话号码
【发布时间】:2015-05-04 21:18:05
【问题描述】:

这是用于获取联系信息的代码:

String id = data.getData().getLastPathSegment();
 Cursor cursor = getActivity().getContentResolver()
                  .query(ContactsContract.Data.CONTENT_URI,
                         new String[] {ContactsContract.Data.DISPLAY_NAME},
                                    ContactsContract.Data.CONTACT_ID + "=?",
                                    new String[]{id},
                                    null);

// short circuit if we didn't pick a contact
     if (cursor.getCount() == 0) {
      return;
     }

     String contact = "";
     String contactName = "";

//code to get contact name
     if (cursor.moveToFirst() && cursor.getString(0) != null) {      
          contact = contact + cursor.getString(0) + ",";
          contactName = cursor.getString(0);
          }
     else {
            contact = contact + ","; //changed
          }
     cursor.close();

 // code to get phone number
   cursor = getActivity().getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                
        newString[{ContactsContract.CommonDataKinds.Phone.NUMBER, 
            ContactsContract.CommonDataKinds.Phone.TYPE},
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
            new String[]{id},null);

      if (cursor.moveToFirst() && cursor.getString(0) != null) {
           contact = contact + cursor.getString(0) + ",";
           contact = contact + cursor.getString(0) + ",";
       } 
      else {
             contact = contact + ",,"; //changed
           }
      cursor.close();

 //Code to get email
    cursor = getActivity().getContentResolver()
            .query(
             ContactsContract.CommonDataKinds.Email.CONTENT_URI,
             new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS},
             ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
             new String[]{id},null);

     if (cursor.moveToFirst() && cursor.getString(0) != null) {         
          contact = contact + cursor.getString(0);
     }
     cursor.close();

   contact = contact.replace("+", "");
   Log.i("TAG", "CONTACT INFO = " + contact);

我想做的事:

在这里,我通过内容提供商从联系人中查询联系人姓名电话号码电子邮件,并将所有三个连接成一个字符串,在字符串中用逗号分隔它们。

问题:

我得到了联系人姓名和电子邮件,没有任何问题。 但是,当我从联系人中选择一些联系人时,返回的电话号码为空

返回空电话号码的联系人是来自我当前位置的不同国家/地区的电话号码。

我现在在加拿大,当我选择加拿大联系人时,我得到了正确的电话号码。

但是,当我从印度选择电话号码时,该号码不会返回,而是会得到空结果。

当我选择带有“12345”或“5555555555”等数字的联系人时,也会发生同样的情况。

我的问题

会不会是Android在尝试验证号码的真实性并返回一个空号码,如果它发现电话号码无效? *(可能不是!一定是我的错!)*

我该如何解决这个问题?我不能像用户在联系人中保存电话号码一样获取电话号码的值吗?

对于代码或我的问题中的任何明确性,我深表歉意。我是一名新手程序员,非常感谢您的所有投入!

【问题讨论】:

    标签: android android-contentprovider android-contacts android-cursor


    【解决方案1】:

    我从该代码中观察到的一件事是,您正在阅读联系人而不使用

    ContactsContract.Contacts.CONTENT_URI
    

    我修改了您的代码并创建了一个新方法来打印所有联系人。

    public static void fetchContacts(Context context) {
    
        String phoneNumber = null;
        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
        StringBuffer output = new StringBuffer();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
    
        if (cursor.getCount() > 0) {
    
            while (cursor.moveToNext()) {
    
                String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
                String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
                long hasPhoneNumber = Long.parseLong(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
    
                if (hasPhoneNumber > 0) {
                    output.append("\n Name:" + name);
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
    
                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append("\n Number:" + phoneNumber);
                    System.out.println("Number:::::"+phoneNumber);
                    System.out.println("Contact:::::"+output.toString());
                }
                    phoneCursor.close();
                }
            }
        }else{
    //          Toast.makeText(context, "No contacts Found", Toast.LENGTH_LONG).show();
            }
    
        }
    

    【讨论】:

    • 我正在尝试运行代码。但是,我无法弄清楚Util.formatMobileNumber()。 Util 来自哪个库?
    • 忽略 Util.formatMobileNumber() ,我用来格式化长度为
    • 有效吗?我几乎有同样的问题。你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多