Autore Topic: SharedPreferences e BackupManager  (Letto 672 volte)

Offline Ohmnibus

  • Utente senior
  • ****
  • Post: 773
  • Respect: +161
    • Github
    • Google+
    • @ohmnibus
    • Mostra profilo
    • Lords of Knowledge GdR
  • Dispositivo Android:
    Huawei P9 Lite
  • Play Store ID:
    Ohmnibus
  • Sistema operativo:
    Windows 10 x64
SharedPreferences e BackupManager
« il: 04 Luglio 2012, 21:53:31 CEST »
0
Salve a tutti,

sto impazzendo dietro un problema incomprensibile.

Ho deciso di implementare il salvataggio dei dati "on the cloud" utilizzando il BackupManager disponibile dalla versione 2.2 di Android.

Ho la necessità di fare backup di un file dove scrivo alcuni dati (circa 3kb) ed ovviamente delle preferenze.

Ho implementato il mio bravo BackupAgent:
Codice (Java): [Seleziona]
import java.io.IOException;

import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.os.ParcelFileDescriptor;

public class BackupAgent extends BackupAgentHelper {
       
        // A key to uniquely identify the set of backup data
    static final String FILES_DICE_BAGS_KEY = "QuickDiceBags";

    // A key to uniquely identify the set of backup preference
    static final String MAIN_PREFERENCES_KEY = "QuickDicePref";
   
        /* (non-Javadoc)
         * @see android.app.backup.BackupAgent#onCreate()
         */

        @Override
        public void onCreate() {
            addHelper(
                        MAIN_PREFERENCES_KEY,
                        new SharedPreferencesBackupHelper(this, this.getPackageName () + "_preferences"));

        addHelper(
                        FILES_DICE_BAGS_KEY,
                        new FileBackupHelper(this, DiceBagManager.FILE_NAME_DICEBAGS));
        }

        /* (non-Javadoc)
         * @see android.app.backup.BackupAgentHelper#onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)
         */

        @Override
        public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
                // Hold the lock while the FileBackupHelper performs backup
            synchronized (DiceBagManager.dataAccessLock) {
                super.onBackup(oldState, data, newState);
            }
        }

        /* (non-Javadoc)
         * @see android.app.backup.BackupAgentHelper#onRestore(android.app.backup.BackupDataInput, int, android.os.ParcelFileDescriptor)
         */

        @Override
        public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
                // Hold the lock while the FileBackupHelper restores the file
            synchronized (DiceBagManager.dataAccessLock) {
                        super.onRestore(data, appVersionCode, newState);
            }
        }

}

Ho modificato il manifest come richiesto, ho impostato la chiave API necessaria ecc. ecc.

La prova che tutto è corretto è data dal fatto che il file di dati viene correttamente backuppato e ripristinato. Mentre le preferenze NO.

Stando al logcat sembra che tutto funzioni a dovere, ma in effetti le preferenze non vengono mai ripristinate.

LogCat del salvataggio dei dati di backup:
Codice: [Seleziona]
07-04 19:20:41.654: D/PerformBackupThread(59): starting agent for backup of BackupRequest{app=ApplicationInfo{44028540 ohm.quickdice} full=false}
07-04 19:20:41.663: D/BackupManagerService(59): awaiting agent for ApplicationInfo{44028540 ohm.quickdice}
07-04 19:20:41.663: D/BackupManagerService(59): agentConnected pkg=ohm.quickdice agent=android.os.BinderProxy@43e863c8
07-04 19:20:41.684: D/BackupHelperDispatcher(647): handling existing helper 'QuickDiceBags' android.app.backup.FileBackupHelper@43f00630
07-04 19:20:41.694: D/BackupHelperDispatcher(647): handling existing helper 'QuickDicePref' android.app.backup.SharedPreferencesBackupHelper@43eb3510
07-04 19:20:41.713: V/LocalTransport(59): performBackup() pkg=ohm.quickdice
07-04 19:20:41.725: V/LocalTransport(59): Got change set key=QuickDiceBags:DiceBags size=3242 key64=UXVpY2tEaWNlQmFnczpEaWNlQmFncw==
07-04 19:20:41.725: V/LocalTransport(59):   data size 3242
07-04 19:20:41.734: V/LocalTransport(59): Got change set key=QuickDicePref:ohm.quickdice_preferences size=128 key64=UXVpY2tEaWNlUHJlZjpvaG0ucXVpY2tkaWNlX3ByZWZlcmVuY2Vz
07-04 19:20:41.744: V/LocalTransport(59):   data size 128
07-04 19:20:41.744: V/LocalTransport(59): finishBackup()
07-04 19:20:41.753: D/PerformBackupThread(59): starting agent for backup of BackupRequest{app=ApplicationInfo{43ffdad8 ohm.quickdice} full=false}
07-04 19:20:41.774: D/BackupManagerService(59): awaiting agent for ApplicationInfo{43ffdad8 ohm.quickdice}
07-04 19:20:41.774: D/BackupManagerService(59): agentConnected pkg=ohm.quickdice agent=android.os.BinderProxy@43fcbe38
07-04 19:20:41.793: D/BackupHelperDispatcher(647): handling existing helper 'QuickDiceBags' android.app.backup.FileBackupHelper@43ec5030
07-04 19:20:41.793: D/BackupHelperDispatcher(647): handling existing helper 'QuickDicePref' android.app.backup.SharedPreferencesBackupHelper@43e564b8
07-04 19:20:41.824: D/PerformBackupThread(59): starting agent for backup of BackupRequest{app=ApplicationInfo{44074398 ohm.quickdice} full=false}
07-04 19:20:41.833: D/BackupManagerService(59): awaiting agent for ApplicationInfo{44074398 ohm.quickdice}
07-04 19:20:41.833: D/BackupManagerService(59): agentConnected pkg=ohm.quickdice agent=android.os.BinderProxy@43e91e20
07-04 19:20:41.854: D/BackupHelperDispatcher(647): handling existing helper 'QuickDiceBags' android.app.backup.FileBackupHelper@43e4a888
07-04 19:20:41.864: D/BackupHelperDispatcher(647): handling existing helper 'QuickDicePref' android.app.backup.SharedPreferencesBackupHelper@43e4aef0
07-04 19:20:41.884: D/PerformBackupThread(59): starting agent for backup of BackupRequest{app=ApplicationInfo{43fc2538 ohm.quickdice} full=false}
07-04 19:20:41.904: D/BackupManagerService(59): awaiting agent for ApplicationInfo{43fc2538 ohm.quickdice}
07-04 19:20:41.904: D/BackupManagerService(59): agentConnected pkg=ohm.quickdice agent=android.os.BinderProxy@43fcbe00
07-04 19:20:41.913: D/BackupHelperDispatcher(647): handling existing helper 'QuickDiceBags' android.app.backup.FileBackupHelper@43e5e468
07-04 19:20:41.924: D/BackupHelperDispatcher(647): handling existing helper 'QuickDicePref' android.app.backup.SharedPreferencesBackupHelper@43e67f68
07-04 19:20:41.944: D/PerformBackupThread(59): starting agent for backup of BackupRequest{app=ApplicationInfo{44064ec8 ohm.quickdice} full=false}
07-04 19:20:41.963: D/BackupManagerService(59): awaiting agent for ApplicationInfo{44064ec8 ohm.quickdice}
07-04 19:20:41.963: D/BackupManagerService(59): agentConnected pkg=ohm.quickdice agent=android.os.BinderProxy@43fbda68
07-04 19:20:41.984: D/BackupHelperDispatcher(647): handling existing helper 'QuickDiceBags' android.app.backup.FileBackupHelper@43e42720
07-04 19:20:41.984: D/BackupHelperDispatcher(647): handling existing helper 'QuickDicePref' android.app.backup.SharedPreferencesBackupHelper@43e5a550
07-04 19:20:42.014: V/LocalTransport(59): finishBackup()


LogCat del ripristino dati (dopo aver reinstallato l'applicazione):
Codice: [Seleziona]
07-04 19:21:52.504: D/BackupManagerService(59): MSG_RUN_RESTORE observer=null
07-04 19:21:52.513: V/LocalTransport(59): start restore 1
07-04 19:21:52.513: V/LocalTransport(59):   nextRestorePackage() = @pm@
07-04 19:21:52.534: V/LocalTransport(59):   getRestoreData() found 7 key files
07-04 19:21:52.534: V/LocalTransport(59):     ... key=@meta@ size=11
07-04 19:21:52.544: V/LocalTransport(59):     ... key=com.android.inputmethod.latin size=1208
07-04 19:21:52.544: V/LocalTransport(59):     ... key=ohm.quickdice size=501
07-04 19:21:52.553: V/LocalTransport(59):     ... key=com.android.browser size=1208
07-04 19:21:52.564: V/LocalTransport(59):     ... key=com.android.providers.userdictionary size=1208
07-04 19:21:52.574: V/LocalTransport(59):     ... key=android size=1208
07-04 19:21:52.574: V/LocalTransport(59):     ... key=com.android.providers.settings size=1208
07-04 19:21:52.604: V/LocalTransport(59):   nextRestorePackage() = ohm.quickdice
07-04 19:21:52.664: I/ActivityManager(59): Start proc ohm.quickdice for backup ohm.quickdice/.util.BackupAgent: pid=710 uid=10035 gids={}
07-04 19:21:52.694: D/BackupManagerService(59): awaiting agent for ApplicationInfo{44093150 ohm.quickdice}
07-04 19:21:52.995: D/BackupManagerService(59): agentConnected pkg=ohm.quickdice agent=android.os.BinderProxy@4402cd60
07-04 19:21:53.004: V/LocalTransport(59):   getRestoreData() found 2 key files
07-04 19:21:53.004: V/LocalTransport(59):     ... key=QuickDiceBags:DiceBags size=3242
07-04 19:21:53.013: V/LocalTransport(59):     ... key=QuickDicePref:ohm.quickdice_preferences size=128
07-04 19:21:53.064: V/LocalTransport(59):   no more packages to restore
07-04 19:21:53.064: V/LocalTransport(59): finishRestore()

Qualcuno sa dirmi dove posso aver sbagliato?
E' possibile che l'app tenti di accedere al file XML contenente i dati delle preferenze "troppo presto" e rubi il lock al BackupManager? Mi sembra strano perché la gestione delle preferenze dovrebbe essere thread safe...

Grazie.
Ohmnibus
Le mie app su Play Store

È stata trovata una soluzione al tuo problema? Evidenzia il post più utile premendo . È un ottimo modo per ringraziare chi ti ha aiutato.