ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android:添加铃声到联系人不适用于我刚添加的联系人,但适用于我之前同步添加的联系人

2019-07-04 15:14:18  阅读:206  来源: 互联网

标签:android ringtone accountmanager


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

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

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

解决方法:

找到了我的问题的答案.我应该尽早提出这些问题,一旦我问它答案就会出现在我面前,即使我在这个问题上工作了好几天.

无论如何,这是正在发生的事情: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);
            }
        }

这个过程很好.

希望这可以在将来帮助其他人.

标签:android,ringtone,accountmanager
来源: https://codeday.me/bug/20190704/1378116.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有