【问题标题】:How to find and prefix all phone number in Contacts Android?如何在 Contacts Android 中查找所有电话号码并为其添加前缀?
【发布时间】:2018-11-13 17:57:57
【问题描述】:

抱歉英语不好! 我想扫描所有联系人的电话号码并为其添加前缀。但是我的代码没有用。它在阅读联系人时错过了很多电话号码。请帮帮我! `

    String[] columns = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};

    ContentResolver cr =getContentResolver();
    Cursor cursor=cr.query(ContactsContract.Contacts.CONTENT_URI, columns,null,null,null); 

    cursor.moveToFirst();
    while(cursor.moveToNext())
    {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
        if(!(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)).endsWith("0"))  )  
        {
            Cursor phones = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
            if(phones.getCount() > 0)
                while (phones.moveToNext())
                {
                    String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[ \\-().]", "");  //this is phone number
                    int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));  
                    String idIndex = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));

                    switch (type)
                    {
                        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                            //prefix number function and write contacts here.
                            break;

                        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                            //prefix number function and write contacts here.
                            break;

                        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                            //prefix number function and write contacts here.
                            break;

                        //some other type here...

                    }
                }
            phones.close();
        }
    }
    cursor.close();`

【问题讨论】:

  • 联系人前缀是什么意思
  • 我的意思是把电话号码的前缀换成另一个前缀,例如:0909xxxxxx -> 032xxxxxx

标签: java android cursor android-contacts phone-number


【解决方案1】:

这里不需要循环中的循环,只需一个查询即可遍历联系人数据库中的所有数字:

String[] projection = new String[] { Phone.NUMBER, Phone.TYPE, Phone._ID };
Cursor phones = cr.query(Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
    String number = phones.getString(0).replaceAll("[ \\-().]", "");  //you can instead use Phone.NORMALIZED_NUMBER if you're using a high-enough API level
    int type = phones.getInt(1);  
    long id = cursor.getLong(2);

    Log.v("LOG", "got phone: " + id + ", " + number + ", " + type);

    prefixNumber(id, number, type);
}
phones.close();

【讨论】:

  • 感谢您回答我的问题。我尝试了您的代码,但它仍然无法正常工作,在我的通讯录中没有收到任何电话号码。您的代码中的“电话”是什么: (String[] projection = new String[] { Phone.NUMBER, Phone.TYPE, Phone._ID };) 你能帮我函数 prefixNumber() 我下面的函数不起作用好吧:
  • 请在我的新答案中查看我的功能
  • 您可以简单地导入android.provider.ContactsContract.CommonDataKinds.Phone,然后在代码中使用Phone,而不是在代码中的任何地方输入ContactsContract.CommonDataKinds.Phone
  • 关于您的updatePhoneNumber 方法,这不是很好。如果您的原始联系人有两个相同类型的电话号码,您的代码会将两部电话更新为相同的新电话号码,从而有效地删除其中一部电话。要解决这个问题,不要使用Data.RAW_CONTACT_ID 作为您的选择,而是使用Phone._ID 并将其与我在上面的答案中从光标获得的id 值进行比较
  • 我试过了,还是没有成功。我无法将电话号码写入通讯录。你能举个例子吗?
【解决方案2】:

`ssss

public void updatePhoneNumber(ContentResolver contentResolver, long  rawContactId, int phoneType, String PhoneNumber) {

    // Create content values object.
    ContentValues contentValues = new ContentValues();

    // Put new phone number value.
    contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, PhoneNumber);

    // Create query condition, query with the raw contact id.
    StringBuffer whereClauseBuf = new StringBuffer();

    // Specify the update contact id.
    whereClauseBuf.append(ContactsContract.Data.RAW_CONTACT_ID);
    whereClauseBuf.append("=");
    whereClauseBuf.append(rawContactId);

    // Specify the row data mimetype to phone mimetype( vnd.android.cursor.item/phone_v2 )
    whereClauseBuf.append(" and ");
    whereClauseBuf.append(ContactsContract.Data.MIMETYPE);
    whereClauseBuf.append(" = '");
    String mimetype = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
    whereClauseBuf.append(mimetype);
    whereClauseBuf.append("'");

    // Specify phone type.
    whereClauseBuf.append(" and ");
    whereClauseBuf.append(ContactsContract.CommonDataKinds.Phone.TYPE);
    whereClauseBuf.append(" = ");
    whereClauseBuf.append(phoneType);

    // Update phone info through Data uri.Otherwise it may throw java.lang.UnsupportedOperationException.
    Uri dataUri = ContactsContract.Data.CONTENT_URI;

    // Get update data count.
    int updateCount = contentResolver.update(dataUri, contentValues, whereClauseBuf.toString(), null);
}`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多