【问题标题】:Android: Adding ringtone to contact doesn't work on a contact I just added, but works on a contact I added on previous syncAndroid:向联系人添加铃声不适用于我刚刚添加的联系人,但适用于我在之前同步时添加的联系人
【发布时间】:2011-12-22 01:43:56
【问题描述】:

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

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

我这辈子都不知道这里发生了什么,有什么想法吗?

【问题讨论】:

    标签: android ringtone accountmanager


    【解决方案1】:

    找到了我的问题的答案。我应该尽快提出这样的问题,似乎我一提出问题就会得到答案,即使我已经为此问题工作了好几天。

    无论如何,这是发生了什么: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);
                }
            }
    

    这个过程运行良好。

    希望这可以帮助将来的其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      相关资源
      最近更新 更多