Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SharedPreferences Backup Not Working
    primarykey
    data
    text
    <p>I've been doing my homework on how to backup SharedPreferences in my Android application, especially using reflection to maintain backwards compatibility. At least I've been trying. Unfortunately, none of my code actually ends up creating a backup! This includes forcing <em>adb bmgr</em> commands on the emulator as explained <a href="http://developer.android.com/guide/topics/data/backup.html" rel="nofollow">here</a>. So I'm wondering if the community could perhaps help me out and in the process come up with some better documentation?</p> <p>Here's my code. To keep this as generic as possible for others, I will simply call my application <strong>Andy</strong> with a package name of <strong>com.example.andy</strong>.</p> <p><strong>Android Manifest</strong> (excerpt)</p> <pre><code>&lt;application ... android:backupAgent="com.example.andy.backupHelper" android:restoreAnyVersion="true"&gt; ... &lt;meta-data android:name="com.google.android.backup.api_key" android:value="GIVEN KEY GOES HERE" /&gt; ... </code></pre> <p><strong>backupHelper.java</strong></p> <p>Note: /data/data/com.example.andy/shared_prefs/com.example.andy_preferences.xml</p> <pre><code>package com.example.andy; import android.app.backup.BackupAgentHelper; import android.app.backup.SharedPreferencesBackupHelper; public class BlinkyBackup extends BackupAgentHelper { static final String PREFS_FILE = "andy_preferences"; static final String BACKUP_KEY = "AndyPreferencesBackup"; public void onCreate() { SharedPreferencesBackupHelper backupHelper = new SharedPreferencesBackupHelper(this, PREFS_FILE); addHelper(BACKUP_KEY, backupHelper); } } </code></pre> <p><strong>BackupAgentWrapper</strong></p> <pre><code>package com.example.andy; import android.app.backup.BackupManager; import android.content.Context; public class BackupAgentWrapper { private BackupManager wrappedInstance; static { try { Class.forName("android.app.backup.BackupManager"); } catch (Exception e) { throw new RuntimeException(e); } } public static void checkAvailable() {} public void dataChanged() { wrappedInstance.dataChanged(); } public BackupAgentWrapper(Context ctx) { wrappedInstance = new BackupManager(ctx); } } </code></pre> <p>And finally, the commands to initiate a backup during run-time. In my application, this code is run from a class available to my application (not the main activity) which is passed <strong>this</strong> as a context upon creation and then stored in the private variable mContext.</p> <pre><code>private void backupData() { boolean backupAgentAvailable = false; try { BackupAgentWrapper.checkAvailable(); backupAgentAvailable = true; } catch (Throwable t) { // really nothing to do } if(backupAgentAvailable) { BackupAgentWrapper backupWrapper = new BackupAgentWrapper(mContext); backupWrapper.dataChanged(); } } </code></pre> <p>To summarize, neither the above function nor the commands below actually backup any data:</p> <pre><code>$ adb shell bmgr enable true $ adb shell bmgr backup com.example.andy $ adb shell bmgr run </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload