Cloud Sync









                                                       - Rahul Singh

INTRO


When you purchase a new device, we hate to lose the High scores of our favorite games on the old device.




Guess what!! Google Backup API is there to help!!


Syncing to the cloud


  • By providing powerful APIs for internet connectivity, the Android framework helps you build rich cloud-enabled apps that sync their data to a remote web service, making sure all your devices always stay in sync, and your valuable data is always backed up to the cloud.
  • All you need to do is integrate the Backup API into your Android Application, so that user data such as preferences, notes, and high scores update seamlessly across all of a user's devices

HOW

                Now you know why we need Cloud Sync, lets see how we integrate it in our app.


         


    Register for the Android Backup Service

    •            Use this link to register your app with the  service.
    Manifest

    Use of the Android Backup Service requires two additions to your application manifest.
    • Declare the name of the class that acts as your backup agent.
    • Add the snippet below as a child element of the Application tag.

    Assuming your backup agent is going to be called TheBackupAgent, here's an example of  what the manifest looks like with this tag included:



    <application android:label="MyApp"
                 
    android:backupAgent="TheBackupAgent">
        ...
       
    <meta-data android:name="com.google.android.backup.api_key"
       
    android:value="ABcDe1FGHij2KlmN3oPQRs4TUvW5xYZ" />
        ...
    </application>

    THe BAckup Agent

    • Lets assume the name of your class is "TheBackupAgent". 
    • Take a little pain and extend  "BackupAgentHelper".
    • Inside the onCreate() method, create a BackupHelper. Android provides two such helpers   FileBackupHelper and SharedPreferencesBackupHelper.
    • After you create the helper and point it at the data you want to back up, just add it to the BackupAgentHelper using the addHelper() method, adding a key which is used to
      retrieve the data later.

    Lets see how we do this...





    import android.app.backup.BackupAgentHelper;
    import android.app.backup.FileBackupHelper;
    
    
     public class TheBackupAgent extends BackupAgentHelper {

      static final String HIGH_SCORES_FILENAME = "scores"; static final String FILES_BACKUP_KEY = "myfiles"; @Override void onCreate() { FileBackupHelper helper = new FileBackupHelper(this, HIGH_SCORES_FILENAME); addHelper(FILES_BACKUP_KEY, helper); } }

     FileBackupHelper's constructor can take a variable number of filenames.  You could just as easily have backed up both a high scores file and a game progress file just by adding an extra parameter, like this :

     

    @Override
        void onCreate() {
            FileBackupHelper helper = new FileBackupHelper(this, HIGH_SCORES_FILENAME, PROGRESS_FILENAME);
            addHelper(FILES_BACKUP_KEY, helper);
        } 




      So, that was about backing up files. Lets backup the SharedPreferences.

    • Create a SharedPreferencesBackupHelper.
    • Instead of adding filenames to the constructor, add the names of the shared preference groups being used by your application.

     
        Lets see it with an example...
    import android.app.backup.BackupAgentHelper;
     import android.app.backup.SharedPreferencesBackupHelper;
    
     public class TheBackupAgent extends BackupAgentHelper {

     
      static final String PREFS_DISPLAY = "displayprefs"; static final String PREFS_SCORES = "highscores"; static final String MY_PREFS_BACKUP_KEY = "myprefs"; void onCreate() { SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS_DISPLAY, PREFS_SCORES); addHelper(MY_PREFS_BACKUP_KEY, helper); } }

    Restore

     We are done with the backup, lets restore the data.


     In order to request a backup, just create an instance of the BackupManager, and call it's  dataChanged() method.


    import android.app.backup.BackupManager;
     ...
    
     public void requestBackup() {
       BackupManager bm = new BackupManager(this);
       bm.dataChanged();
     } 

    Terminal TIps

     Ensure data backup is enabled.

    adb shell bmgr enable true

     Performing backup

    adb shell bmgr backup your.package.name
    adb shell bmgr run

     Uninstall and reinstall your application

    adb uninstall your.package.name










                                                                           Thanks :-)

    CloudSync

    By Torry Harris Business Solutions