gpt4 book ai didi

java - 检查并更新 Android 中的电话簿

转载 作者:行者123 更新时间:2023-12-01 09:01:49 24 4
gpt4 key购买 nike

我正在尝试更新电话簿中的联系人,但我需要检查联系人号码是否在 TYPE_WORK 、 TYPE_MOBILE 等标题下......

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

//ASSERT: @contactId alreay has a work phone number
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


String selectPhone = ContactsContract.CommonDataKinds.Phone.NUMBER + "=? AND " + ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";

String[] phoneArgs = new String[]{oldNumber, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};

if(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs).equals(""))
{
Toast.makeText(getContext(), "MOBILE FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
phoneArgs = new String[]{oldNumber, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
if(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs).equals(""))
{
Toast.makeText(getContext(), "Work FIELD IS EMPTY", Toast.LENGTH_SHORT).show();

}
else
{
Toast.makeText(getContext(), "Work FIELD IS NOT EMPTY", Toast.LENGTH_SHORT).show();

ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
.build());
}
}
else {
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
.build());
}

getContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}

我尝试过但没有成功..PLs 帮助

最佳答案

您需要检索联系人 ID 和联系人类型,并在更新查询参数中使用它。我尝试了这个,它有效。 -

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

// get contact type and contact id
String contactId = null;
int contactType = -1;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(oldNumber));
Cursor cursor = getContentResolver().query(
uri,
new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.TYPE},
null,
null,
null);

if (cursor != null) {
while (cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
contactType = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.TYPE));
}
cursor.close();
}

//ASSERT: @contactId alreay has a work phone number
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


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

String[] phoneArgs = null;

if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
Toast.makeText(this, "MOBILE FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
phoneArgs = new String[]{contactId, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
} else if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
Toast.makeText(this, "Work FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
phoneArgs = new String[]{contactId, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
}
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
.build());


ContentProviderResult[] contentProviderResults = this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (contentProviderResults[0].count > 0) {
Toast.makeText(this, "Updated Contact Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Contact could not be updated", Toast.LENGTH_SHORT).show();
}
}

确保您拥有读取和写入联系人的权限。希望这有帮助!

关于java - 检查并更新 Android 中的电话簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41607769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com