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

私の質問に対する答えが見つかりました。SO の質問をもっと早くする必要があります。問題に何日も取り組んでいても、質問するとすぐに答えが返ってくるようです。

とにかく、ここで何が起こっているのか: ringtoneSync メソッドは、addContact() メソッドを実行したときに作成される rawContactId を探しています。問題は、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 に答える