Как найти и префикс всего номера телефона в контактах Android? - PullRequest
0 голосов
/ 04 июня 2018

Извините за плохой английский!Я хочу отсканировать номер телефона всех контактов и поставить перед ними префикс.Однако мой код не является полезным.Он пропустил много телефонных номеров при чтении контактов.Помоги мне, плз!`

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

Ответы [ 2 ]

0 голосов
/ 19 июня 2018

`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);
}`
0 голосов
/ 17 июня 2018

Здесь не нужен цикл внутри цикла, только один запрос, который обрабатывает все числа в БД контактов:

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