Generate backup on Google backup services in Android

In this article, we are going to generate backup of our application by using Google backup services of Android.
Submitted by Manu Jemini, on February 06, 2018

First of all, we need to set two strings that clarify the preferences and backup key which are following:

    String file_name_for_setting_preferences = "ourpreferences";
    String preferences_backup_key = "bkup";

After that, we create a onCreate() method and use class SharedPreferencesBackupHelper to create an object and pass this as current context and our preferences string then call addHelper() method and pass backupkey and SharedPreferencesBackupHelper class object.

For backup key:

For backup key, you just need to go on the official site of android and register your app on google backup services. The URL for registration on official site is: https://developer.android.com/google/backup/signup.html

After entering your app package name you are eligible for registering on google backup services. After further process google backup services provide you a backup key generate backup of your application data, which you can use in your Android Manifest file as backup key.

Note: In below Android Manifest file, first you need to copy your google backup key and paste it here in android:value="Your key from google services" as android value.

After this, you are all set to go with your google backup services.

1) Java file:

package com.example.hp.myapplication;

import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;

public class MainActivity extends BackupAgentHelper {
	String file_name_for_setting_preferences = "ourpreferences";
	String preferences_backup_key = "bkup";

	@Override
	public void onCreate() {
		SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this,
				file_name_for_setting_preferences);
		addHelper(preferences_backup_key, helper);
	}
}

2) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:backupAgent="MainActivity"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.backup.api_key"
            android:value="Your key from google services" />

    </application>

</manifest>

3) String.xml:

<resources>
    <string name="app_name">My Application</string>
</resources>

Here is the command to enable your data backup in Android after doing above things: adb shell bmgr enable true

Next (this) command will enable the auto backup facility and to disable it you need to type: adb shell bmgr enable false

After doing all of this, you are all set for backing up data on Google backup services.


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.