Backup Manager

The project describes how to implement BackupManager for your application so that you can save the current state of your game/application.

Underlying Algorithm:

Basic description of algorithm in step by step form:

1.) Create a Project BackupManager
2.) Open and insert following in layout/backup_restore.xml:

 

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">

android:text="@string/bacon_label"/>
android:text="@string/pastrami_label"/>
android:text="@string/hummus_label"/>

android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

android:text="@string/mayo_text"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

android:text="@string/tomato_text"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<button> android:text="@string/restore_text"
android:onClick="onRestoreButtonClick"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0" />

 

 

3.) Open and insert following in string.xml:

 

Hello World, BackupManager!
BackupManager
Choose Settings for your game:
Sound On
Vibration On
Backlight On
Extras:
Use Orientation?
Use Camera?
Restore last data

 

 

4.) Your manifest file will look like:

 

android:backupAgent="ExampleAgent">
<!-- Some backup transports may require API keys or other metadata -->
android:value="INSERT YOUR API KEY HERE" />

 

 

5.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. BackupManager. Enter following information:

Project name: BackupManager
Build Target: Android 2.3.3
Application name: BackupManager
Package name: org.example.BackupManager
Create Activity: BackupManager

On Clicking Finish BackupManager code structure is generated with the necessary Android Packages being imported along with BackupManager.java. BackupManager class will look like following:

 

package org.example.BackupManagerExample;

import android.app.Activity;
import android.app.backup.BackupManager;
import android.app.backup.RestoreObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class BackupManagerExample extends Activity {
static final String TAG = "BRActivity";

static final Object[] sDataLock = new Object[0];

static final String DATA_FILE_NAME = "saved_data";

RadioGroup mFillingGroup;
CheckBox mAddMayoCheckbox;
CheckBox mAddTomatoCheckbox;

File mDataFile;

BackupManager mBackupManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.backup_restore);

mFillingGroup = (RadioGroup) findViewById(R.id.filling_group);
mAddMayoCheckbox = (CheckBox) findViewById(R.id.mayo);
mAddTomatoCheckbox = (CheckBox) findViewById(R.id.tomato);

mDataFile = new File(getFilesDir(), BackupManagerExample.DATA_FILE_NAME);

mBackupManager = new BackupManager(this);

populateUI();
}

void populateUI() {
RandomAccessFile file;

int whichFilling = R.id.pastrami;
boolean addMayo = false;
boolean addTomato = false;

synchronized (BackupManagerExample.sDataLock) {
boolean exists = mDataFile.exists();
try {
file = new RandomAccessFile(mDataFile, "rw");
if (exists) {
Log.v(TAG, "datafile exists");
whichFilling = file.readInt();
addMayo = file.readBoolean();
addTomato = file.readBoolean();
Log.v(TAG, " mayo=" + addMayo
+ " tomato=" + addTomato
+ " filling=" + whichFilling);
} else {
Log.v(TAG, "creating default datafile");
writeDataToFileLocked(file,
addMayo, addTomato, whichFilling);

mBackupManager.dataChanged();
}
} catch (IOException ioe) {

}
}

mFillingGroup.check(whichFilling);
mAddMayoCheckbox.setChecked(addMayo);
mAddTomatoCheckbox.setChecked(addTomato);

mFillingGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group,
int checkedId) {
Log.v(TAG, "New radio item selected: " + checkedId);
recordNewUIState();
}
});

CompoundButton.OnCheckedChangeListener checkListener
= new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.v(TAG, "Checkbox toggled: " + buttonView);
recordNewUIState();
}
};
mAddMayoCheckbox.setOnCheckedChangeListener(checkListener);
mAddTomatoCheckbox.setOnCheckedChangeListener(checkListener);
}

void writeDataToFileLocked(RandomAccessFile file,
boolean addMayo, boolean addTomato, int whichFilling)
throws IOException {
file.setLength(0L);
file.writeInt(whichFilling);
file.writeBoolean(addMayo);
file.writeBoolean(addTomato);
Log.v(TAG, "NEW STATE: mayo=" + addMayo
+ " tomato=" + addTomato
+ " filling=" + whichFilling);
}

void recordNewUIState() {
boolean addMayo = mAddMayoCheckbox.isChecked();
boolean addTomato = mAddTomatoCheckbox.isChecked();
int whichFilling = mFillingGroup.getCheckedRadioButtonId();
try {
synchronized (BackupManagerExample.sDataLock) {
RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
writeDataToFileLocked(file, addMayo, addTomato, whichFilling);
}
} catch (IOException e) {
Log.e(TAG, "Unable to record new UI state");
}

mBackupManager.dataChanged();
}

public void onRestoreButtonClick(View v) {
Log.v(TAG, "Requesting restore of our most recent data");
mBackupManager.requestRestore(
new RestoreObserver() {
public void restoreFinished(int error) {
Log.v(TAG, "Restore finished, error = " + error);
populateUI();
}
}
);
}
}

 

 

Output – The final output:

Leave a Comment:

7 comments
Nobi says September 13, 2011

it have several mistake, I think you miss something on the topic. Miss the button Save and Some method ex: mBackupManager.requestRestore. Please complete the Example

Reply
Monica says January 4, 2012

Hello ,
The only demo which we could find.Tried to run the demo but giving a security exception as “java.lang.SecurityException: getCurrentTransport: Neither user 10065 nor current process has android.permission.BACKUP.”
Kindly let me know if there is a solution to this .

Reply
Monica says January 4, 2012

Hello ,
The only demo which we could find.Tried to run the demo but giving a security exception as "java.lang.SecurityException: getCurrentTransport: Neither user 10065 nor current process has android.permission.BACKUP."
Kindly let me know if there is a solution to this .

Reply
Monica says January 18, 2012

Thanks..problem solved

Reply
Avi says July 31, 2012

it is a nice tutorial but the code is not working , here i have posted my question can you help me please.

Reply
Avi says July 31, 2012

it is a nice tutorial but the code is not working , here i have posted my question can you help me please. The questin is

http://stackoverflow.com/questions/11735925/error-neither-user-10066-nor-current-process-has-android-permission-backup

Reply
Avi says July 31, 2012

it is a nice tutorial but the code is not working , here i have posted my question can you help me please. The question is

http://stackoverflow.com/questions/11735925/error-neither-user-10066-nor-current-process-has-android-permission-backup

Reply
Add Your Reply