Android 使用多个电话号码更新联系人

Android 使用多个电话号码更新联系人,android,android-contentprovider,android-contacts,Android,Android Contentprovider,Android Contacts,我有这个密码 public void updateContact (String contactId, String newNumber) {ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" +

我有这个密码

public void updateContact (String contactId, String newNumber) {ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + 
                     Phone.CONTENT_ITEM_TYPE + "'"; 

    String[] phoneArgs = new String[]{contactId};
    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                    .withSelection(selectPhone, phoneArgs)
                    .withValue(Phone.NUMBER, newNumber)
                    .build()); 
            try { getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }
public void updateContact(String contactId,String newNumber){ArrayList ops=new ArrayList();
字符串选择Phone=Data.CONTACT_ID+“=”和“+Data.MIMETYPE+”=”+
Phone.CONTENT_ITEM_TYPE+“”;
字符串[]phoneArgs=新字符串[]{contactId};
添加(ContentProviderOperation.newUpdate(Data.CONTENT\u URI)
.通过选择(选择Phone,phoneArgs)
.withValue(电话号码、新号码)
.build());
尝试{getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(操作应用程序异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}       
}
我从这里取来并更改了它,以便通过我的所有联系人,并在每个联系人中添加我自己的前缀

我浏览了所有联系人,并提供了这些信息
String id=phones.getString(phones.getColumnIndex(ContactsContract.Contacts.\u id))

上述功能的联系人id。但是有些联系人,特别是有多个号码的联系人和只有一个号码的联系人,即使给定的id是正确的,也不会更改以获得新的前缀。 我错过了什么吗?我不知道该怎么改变。我认为可能是mime类型,但我可以想象,可能有人没有在手机上使用Android预装类型,而是使用自定义类型。
我没有得到任何错误。谢谢大家的时间

我最终更改了代码以正常工作,我正在传递联系人ID,但我需要电话ID…这是正确的代码

public void updateContact (String contactId,  String newNumber) {        


    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    String selectPhone = Data._ID + "=? AND " + Data.MIMETYPE + "='"  + 
                         Phone.CONTENT_ITEM_TYPE + "'"; 

            String[] phoneArgs = new String[]{contactId};
            ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                    .withSelection(selectPhone, phoneArgs)
                    .withValue(Phone.NUMBER, newNumber)
                    .build()); 
            try {
                getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }   
public void updateContact(String contactId,String newNumber){
ArrayList ops=新的ArrayList();
字符串选择Phone=Data._ID+“=”和“+Data.MIMETYPE+”=”+
Phone.CONTENT_ITEM_TYPE+“”;
字符串[]phoneArgs=新字符串[]{contactId};
添加(ContentProviderOperation.newUpdate(Data.CONTENT\u URI)
.通过选择(选择Phone,phoneArgs)
.withValue(电话号码、新号码)
.build());
试一试{
getContentResolver().applyBatch(contacts contract.AUTHORITY,ops);
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(操作应用程序异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}       
}   

别忘了在android清单(写联系人)上设置适当的权限。

我最终更改了代码以正常工作,我传递了联系人ID,但我需要手机ID…以下是正确的代码

public void updateContact (String contactId,  String newNumber) {        


    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    String selectPhone = Data._ID + "=? AND " + Data.MIMETYPE + "='"  + 
                         Phone.CONTENT_ITEM_TYPE + "'"; 

            String[] phoneArgs = new String[]{contactId};
            ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                    .withSelection(selectPhone, phoneArgs)
                    .withValue(Phone.NUMBER, newNumber)
                    .build()); 
            try {
                getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }   
public void updateContact(String contactId,String newNumber){
ArrayList ops=新的ArrayList();
字符串选择Phone=Data._ID+“=”和“+Data.MIMETYPE+”=”+
Phone.CONTENT_ITEM_TYPE+“”;
字符串[]phoneArgs=新字符串[]{contactId};
添加(ContentProviderOperation.newUpdate(Data.CONTENT\u URI)
.通过选择(选择Phone,phoneArgs)
.withValue(电话号码、新号码)
.build());
试一试{
getContentResolver().applyBatch(contacts contract.AUTHORITY,ops);
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(操作应用程序异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}       
}   
别忘了在android清单上设置适当的权限(写联系人)