所以我正在进行帐户同步,并且包含在该过程中的是添加自定义铃声的步骤。这是我添加铃声的方法:
private static void ringtoneSync(ContentResolver resolver, String username, Context context) {
ContentValues values = new ContentValues();
Log.e("SYNC", "setting ringtone for " + username);
long rawContactId = lookupRawContact(resolver, username);
long contactId = getContactId(resolver, rawContactId);
File root = Environment.getExternalStorageDirectory();
TagDBAdapter adapter = new TagDBAdapter(context);
adapter.open();
String ringtone = adapter.getContactRingtonePath(username);
adapter.close();
Log.e("test", "ringtone checkpoint name here: " + ringtone);
File file = new File(root, "tag/ringtones/"+ ringtone + ".mp3");
if(file.exists()) {
Log.e("test", "ringtone checkpoint if file exists");
Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, ringtone);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
Uri newUri = resolver.insert(uri, values);
String uriString = newUri.toString();
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
Log.e("Uri String for " + username, uriString);
resolver.update(ContactsContract.Contacts.CONTENT_URI, values, Contacts._ID + "=" + contactId, null);
}
}
此方法效果很好,除非我事先第一次向帐户添加联系人。我对添加联系人的呼吁结构如下:
for(Contact contact : friends) {
Log.e("SYNCING CONTACTS", "Start for loop");
username = contact.getUsername();
rawContactId = lookupRawContact(resolver, username);
if(rawContactId != 0) {
if(!contact.isDeleted()) {
Log.e("SYNCING CONTACTS", "Updating " + username);
updateContact(context, resolver, account, contact, rawContactId, batchOperation);
ringtoneSync(resolver, username, context);
}
else {
Log.e("SYNCING CONTACTS", "Deleting " + username);
deleteContact(context, rawContactId, batchOperation);
}
}
else {
if(!contact.isDeleted()) {
Log.e("SYNCING CONTACTS", "Adding " + username);
addContact(context, account, contact, batchOperation);
ringtoneSync(resolver, username, context);
}
}
因此,您可以看到它的调用非常相似,无论它是新的还是现有的联系人,但它实际上仅适用于现有联系人。更重要的是,即使没有成功添加铃声,我作为检查点输入的所有日志行都会在logcat中准确显示。
我无法弄清楚我的生活在这里发生了什么,有什么想法吗?
答案 0 :(得分:2)
找到我的问题的答案。我应该尽快提出这些问题,一旦我问它答案就会出现在我面前,即使我在这个问题上工作了好几天。
无论如何,这是正在发生的事情:ringtoneSync方法正在寻找rawContactId,它是在执行addContact()方法时创建的。问题是,在调用batchOperation.execute()之前,不会提交rawContactId。
所以通过更改我的联系人添加循环:
if(rawContactId != 0) {
if(!contact.isDeleted()) {
Log.e("SYNCING CONTACTS", "Updating " + username);
updateContact(context, resolver, account, contact, rawContactId, batchOperation);
ringtoneSync(resolver, username, context);
}
else {
Log.e("SYNCING CONTACTS", "Deleting " + username);
deleteContact(context, rawContactId, batchOperation);
}
}
else {
if(!contact.isDeleted()) {
Log.e("SYNCING CONTACTS", "Adding " + username);
addContact(context, account, contact, batchOperation);
ringtoneSync(resolver, username, context);
}
}
对此:
if(rawContactId != 0) {
if(!contact.isDeleted()) {
Log.e("SYNCING CONTACTS", "Updating " + username);
updateContact(context, resolver, account, contact, rawContactId, batchOperation);
ringtoneSync(resolver, username, context);
}
else {
Log.e("SYNCING CONTACTS", "Deleting " + username);
deleteContact(context, rawContactId, batchOperation);
}
}
else {
if(!contact.isDeleted()) {
Log.e("SYNCING CONTACTS", "Adding " + username);
addContact(context, account, contact, batchOperation);
/* -------> */ batchOperation.execute(); //EXECUTE BATCH OPERATION BEFORE SYNCING RINGTONE
ringtoneSync(resolver, username, context);
}
}
这个过程很好。
希望将来可以帮助其他人。