4

所以我正在做帐户同步,并且包含在该过程中的是添加自定义铃声的步骤。这是我添加铃声的方法:

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 中准确显示。

我无法弄清楚这里发生了什么,有什么想法吗?

4

1 回答 1

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);
            }
        }

该过程工作正常。

希望这可以帮助其他人在未来。

于 2011-12-21T17:43:56.787 回答