更改每个联系号码

时间:2013-08-03 20:58:27

标签: android contacts android-contacts

我想创建一个应用程序,查找每个人的电话号码,删除特殊字符,从开始删除0(如果有),拆分前缀和sufix(最后8个数字),然后在中间添加9其中,显示在列表中并更新人员信息。一切都很好,但它不会更新列表中的每个人。该程序显示它更新了人员A,人员B和人员C,但是当我检查时,只更新了B。

    public void btList(View view) {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        list.clear();
        while (phones.moveToNext())
        {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
            int id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
            int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            phoneNumber = phoneNumber.replace(" ", "");
            phoneNumber = phoneNumber.replace("-", "");
            phoneNumber = phoneNumber.replace("+", "");
            phoneNumber = phoneNumber.replace("*", "");
            phoneNumber = phoneNumber.replace("#", "");
            String first = phoneNumber.substring(0, 1);
            boolean comeco;
            String renum;
            if(first.equals("0")){
                //se começar com 0
                renum = phoneNumber.substring(1);
                comeco = true;
            } else {
                //se não começar
                renum = phoneNumber;
                comeco = false;
            }
            long num = Long.parseLong(renum.toString());
            long prenum = num / 100000000;
            long posnum = num % 100000000;
            boolean cidade;
            if(String.valueOf(prenum).startsWith("55"))
                prenum = Long.parseLong(String.valueOf(prenum).substring(2));
            if(prenum != 0){
                if(String.valueOf(prenum).contains("18") || String.valueOf(prenum).contains("9090")){
                    cidade = true;
                }else{
                    cidade = false;
                }
            }else{
                cidade = true;
            }
            if((posnum / 10000000 == 9) || (posnum / 10000000 == 8) && cidade){
                String fnum = null;
                    if(comeco){
                            fnum = "0" + prenum + " 9 " + (posnum / 10000) + "-" + (posnum % 10000);
                            String texto = "Nome: " + name + " / Num: " + fnum;     
                            list.add(texto);


            } else {                
                    if(prenum != 0) {
                        fnum = "" + prenum + " 9 " + (posnum / 10000) + "-" + (posnum % 10000); 
                    } else {
                        fnum = "9 " + (posnum / 10000) + "-" + (posnum % 10000);  
                    }
                    String texto = "Nome: " + name + " / Num: " + fnum;     
                    list.add(texto);
                }
            try {
                updateContact(String.valueOf(id), fnum, String.valueOf(type));
            } catch (RemoteException e) {
                Log.i("UPDATE", "ERRO: " + e);
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                Log.i("UPDATE", "ERRO: " + e);
                e.printStackTrace();
            }
            }
        }
        ListView lista = (ListView) findViewById(R.id.listView1);
        final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        lista.setAdapter(adapter);
        phones.close();
    }

    public void updateContact (String contactId, String newNumber, String phoneType) 
            throws RemoteException, OperationApplicationException{

                //ASSERT: @contactId alreay has a work phone number 
                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
                String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + 
                                Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
                String[] phoneArgs = new String[]{contactId, phoneType}; 
                ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                        .withSelection(selectPhone, phoneArgs)
                        .withValue(Phone.NUMBER, newNumber)
                        .build()); 
                this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            }

}

0 个答案:

没有答案