java android error向宗地写入异常

问题描述

| 请帮助我要更新电话人的电话号码,但会出错 06-25 17:43:57.559:错误/ DatabaseUtils(97):将异常写入包裹 06-25 17:43:57.559:错误/ DatabaseUtils(97):java.lang.UnsupportedOperationException:未知uri:content://com.android.contacts/contacts/1/phones
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;

@SuppressWarnings(\"deprecation\")
public class ChangeContacts extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null);
        if (cur.getCount() > 0) {
        while (cur.movetoNext()) {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.disPLAY_NAME));
        System.out.println(name);

        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {





            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +\" = ?\",new String[]{id},null);
            Uri PersonUri=null;
            Uri PhoneUri = null;
            ContentValues values = new ContentValues();
                    while (pCur.movetoNext()) {
                        String phhoneNumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String contactId=pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts._ID));
                        System.out.println(contactId);



 PersonUri=ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,Long.valueOf(contactId));
PhoneUri = Uri.withAppendedpath(PersonUri,People.Phones.CONTENT_DIRECTORY);


values.clear();

values.put(People.Phones.TYPE,People.Phones.TYPE_MOBILE);
values.put(People.Phones.NUMBER,\"123456789\");


 int upv=getContentResolver().update(PhoneUri,values,null); 
                        System.out.println(upv);
                    System.out.println(phhoneNumber);
                    } 
                    pCur.close();





            }
            }
    }




    }


}
    

解决方法

        问题解决了,我写了这个功能来更新联系人手机号码 phoneId-phoneId(我们要更改此值)
private void updateRecord(int phoneId,String number) {
    Uri uri = ContentUris.withAppendedId(Phones.CONTENT_URI,phoneId);
        values = new ContentValues();
        values.put(People.Phones.TYPE,People.Phones.TYPE_MOBILE);
        values.put(People.Phones.NUMBER,number);
        getContentResolver().update(uri,values,null,null);
    }