安卓:添加铃声联络上我只是增加了一个触点不工作,但工作在我的previous同步添加联系人 [英] Android: Adding ringtone to contact doesn't work on a contact I just added, but works on a contact I added on previous sync
问题描述
所以,我做的帐户同步,并包括在这个过程就是添加自定义铃声的一个步骤。这里是我的方法添加铃声:
So I am doing Account Sync, and included in that process is a step where a custom ringtone is added. Here is my method for adding a ringtone:
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);
}
}
当我事先将联系人添加到该帐户的第一次,除了这种方法的伟大工程。我要添加联系人通话的结构是这样的:
This method works great, except when I am adding a contact to the account for the first time beforehand. My call to adding contacts is structured like this:
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的准确显示这些日志行。
So as you can see it is called very similarly regardless if it is a new or existing contact, but it only actually works for an existing contact. What's more, all those log lines I entered in as checkpoints are displayed accurately in logcat even when the ringtone is not successfully added.
这是怎么回事就在这里,有什么想法我不为我的生活吗?
I can't figure out for the life of me what is going on here, any thoughts?
推荐答案
找到答案我的问题。我要问的问题SO越早,它只要我问它的答案都交给我看来,即使我的工作在这个问题上的日子。
Found an answer to my question. I should ask SO questions sooner, it seems as soon as I ask it the answer comes to me, even if I was working on the issue for days.
不管怎么说,这里正在发生的事情:在ringtoneSync方法是找一个rawContactId,当你做的addContact()方法时创建的。问题在于,rawContactId不提交,直到调用batchOperation.execute()。
Anyways, here is what is going on: the ringtoneSync method is looking for a rawContactId, which is created when you do the addContact() method. Problem is, the rawContactId is not committed until you call batchOperation.execute().
所以,从这个改变我的联系方式添加循环:
So by changing my contact adding loop from this:
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);
}
}
这个过程正常工作。
The process works fine.
希望这可以帮助别人出未来。
Hope this can help someone else out in the future.
这篇关于安卓:添加铃声联络上我只是增加了一个触点不工作,但工作在我的previous同步添加联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!