请帮助我想更新手机用户的电话号码,但收到错误
06-25 17:43:57.559:ERROR / DatabaseUtils(97):向包裹写入异常 06-25 17:43:57.559:ERROR / DatabaseUtils(97):java.lang.UnsupportedOperationException:Unknown 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, 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,
null,
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, null);
System.out.println(upv);
System.out.println(phhoneNumber);
}
pCur.close();
}
}
}
}
}
答案 0 :(得分:1)
问题解决了我为更新联系人手机号码写了这个功能
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);
}