开发者

Can't get BackupAgent to work when reinstall from Android Market

开发者 https://www.devze.com 2023-04-01 21:35 出处:网络
I have implemented the BackupAgentHelper according to the documentation, including backward compatibility for 1.6, to backup all my SharedPreferences. It is working just fine when testing it in emulat

I have implemented the BackupAgentHelper according to the documentation, including backward compatibility for 1.6, to backup all my SharedPreferences. It is working just fine when testing it in emulator A开发者_运维百科ndroid 2.2 and when running on my device, which runs stock Android 2.3.3, via USB. In both these test cases I issue: adb shell bmgr run, which triggers the backup.

However, when reinstalling my app from Android Market, all SharedPreferences are gone. The documentation says after that dataChanged() has been called, "The Backup Manager then calls your backup agent's onBackup() method at an opportune time in the future". Fine, but does anyone knows what makes the system to actually do the backup? I suspect this is not happening in my case.

The documentation says: "If using a device, open the system Settings, select Privacy, then enable Back up my data and Automatic restore." but that option is not present in my device (SE Xperia Mini). I can only guess that this option is always enabled and cannot be turned off. I am using Telia in Sweden as my operator (if that matters).

Anyone got any idea of why the backup is not working when reinstall from Market but fully functioning when using emulator and on the device via USB?


Backup and restore settings in Privacy settings is vendor specific. See this code fragment taken from the Privacy class:

    // Vendor specific
    if (getPackageManager().resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
        screen.removePreference(findPreference(BACKUP_CATEGORY));
    }

Sony Ericsson replaced conditional statement with its contents and it removes this Preference unconditionally.

This code is covered by Apache Licence, therefore SE don't need to supply you source code. If you are live in country, where reverse engineering for this purpose is legal I believe (I'm not lawyer) you can:

  1. Pull Settings package from your device:

    adb pull /system/app/Settings.apk Settings.apk
    
  2. Decode settings.apk using apktool:

    apktool d -s Settings.apk
    
  3. In newly created Settings directory you'll find classes.dex. To decompile it using Smali (can be found on code.google.com):

    baksmali classes.dex -o classes
    
  4. Finally you are interested in classes/com/android/settings/PrivacySettings.smali

You can do same with Settings.apk taken from Android Emulator and compare these two files.

I have Xperia mini pro (SK17i) too and that preference isn't there either.


My previous attempt in testing the functionality when using the Market was by doing it all in a (relatively rapid?) sequence. I even restarted my phone after changing some parameters in SharedPreferences and then uninstalled my app, thinking that a shutdown would trigger pending backup sync actions. Unfortunately, that did not trigger the sync to the backup servers and the SharedPreferences were lost.

Now to the good and informative part. I changed some SharedPreferences and let the phone be for a couple of hours before uninstalling my app. The next time I installed it from the Market, the SharedPreferences were restored from the cloud backup! So the solution was to just make the "opportune time in the future" come to happen. I don't like the behaviour but can accept it as I do realize that it can't sync to the servers every time a dataChanged() is called.

At least now we know that even if "Back up my data and Automatic restore" option under Privacy is not present on your device, it will still do the backup using the BackupAgent. It just need some (undefined amount of) time to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消