2011-12-21 41 views
4

因此,我正在进行帐户同步,并且包含在该过程中是自定义铃声的步骤添加。这是我添加铃声的方法:Android:为联系人添加铃声不适用于我刚刚添加的联系人,但适用于我之前同步添加的联系人

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

我不知道我的生活发生了什么,有什么想法吗?

回答

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

的过程正常工作。

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