繁体   English   中英

如何在通讯录Android中查找所有电话号码并添加前缀?

[英]How to find and prefix all phone number in Contacts Android?

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

    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();`

这里不需要循环内循环,只需查询一个查询即可遍历Contacts DB中的所有号码:

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();

`sssss

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);
}`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM