• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using the BackupAgentHelper to backup AND restore a string?

 
Niklas Karlsson
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a sharedPreferences that I whant to save using the BackupAgentHelper and so on..

but i cant get this to work
(or maby I test this WRONG..)
I first get the string appSettings from my Preferences..
(this is in my activity)
appSettings = loadSavedPreferences();
System.out.println("appSettings is : " + appSettings );
try {
backupdata(appSettings );
} catch (Exception e) {
e.printStackTrace();
}

.....
private String loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String test = sharedPreferences.getString("appSettings", getString(R.string.emty_settingsifnew));
return test;
}



in the backupdata() i pas in a string;
(this is altso in the activity)
private void backupdata(String appSettings2) {
edit.putString("appSettings ", appSettings2 );
edit.commit();
Log.d("BACKUP", "Calling backup...");
backupManager.dataChanged();
}




then in a other Class:


import android.app.backup.BackupAgentHelper;
import .....(and so on..)

public class MyBackupAgent extends BackupAgentHelper {

static final String PREFS_TEST = "testprefs";

static final String MY_PREFS_BACKUP_KEY = "myprefs";

private String appSettings;

// Simply allocate a helper and install it
public void onCreate() {
SharedPreferencesBackupHelper helper =
new SharedPreferencesBackupHelper(this, PREFS_TEST);
addHelper(MY_PREFS_BACKUP_KEY, helper);
Log.d("Test", "Adding backupagent...");
}


}
}




and in the manifest:
(the internet connection premision have i added..)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" android:backupAgent="MyBackupAgent" >

<meta-data android:name="com.google.android.backup.api_key" android:value="Awwwwwwwwwwwweeeeeeeeeeeeeeeexxxxxxxxxxxxxxx....g" />




(the key have I registrate..)


please note :
I have NOt a account in "googledev app site ", I have only registrate on:
http://code.google.com/android/backup/signup.html

and I have tryed to read :
http://developer.android.com/guide/topics/data/backup.html

but i cant understand the problem..


.....

when I run my app (with a internetconnection) thrue eclipse and a USBcable and do a settings and call "backupdata()"
and then uninstall the app.

and then reinstall the app (from eclipse to a mobile)
and run the app and try to do this:

backupManager = new BackupManager(this);
prefs = getSharedPreferences(MyBackupAgent.PREFS_TEST, Context.MODE_PRIVATE);
edit = prefs.edit();
appSettings = prefs.getString("appSettings", "");

i recive ""

and not the last settings :-(

do you have any ideas?

best regards to you all!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic