sneha
sneha

Reputation: 31

unable to call onbackup method in android

I want to implement backup and recovery but it does not execute onBackup() method. My code in manifest file is

<application android:icon="@drawable/icon" android:label="@string/app_name"
        android:backupAgent="MyBackupAgent" android:restoreAnyVersion="true"
        android:allowBackup="true" android:enabled="true">
        <meta-data android:name="com.google.android.backup.api_key"
            android:value="AEdPqrEAAAAIMiLZ2_rMQFv6Huz3BYWpuxfVoK68Wk0CFMiXwA" />
        <activity android:name=".Splash" android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

In Activity when I add or update any item I want to take backup so my code is

BackupManager bm = new BackupManager(this);
bm.dataChanged();

and in MyBackupAgent file I wrote

public class MyBackupAgent extends BackupAgent {

    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) throws IOException {
        // TODO Auto-generated method stub
        System.out.println("in MyBackupAgent");
    }

    @Override
    public void onRestore(BackupDataInput data, int appVersionCode,
            ParcelFileDescriptor newState) throws IOException {
        // TODO Auto-generated method stub

    }
}

But here in onBackup() method it does not print anything so how can I identify whether it's enter in onbackup method or not?

Upvotes: 2

Views: 1632

Answers (5)

Marc
Marc

Reputation: 1189

I had the exact same problem and adding

static 
{
    try
    {
            Class.forName("android.app.backup.BackupManager");
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}

inside my MyBackupAgent class solved the problem. Before that my onBackup was never called. I finally figured it out after stumbling upon Android Book Worm which seems to be a working example.

I also changed a line in the AndroidManifest.xml from

android:backupAgent="MyBackupAgent"

to

android:backupAgent=".MyBackupAgent"

but I am not sure if that did anything.

Here is the link that helped me: ohloh.net code search for android:backupAgent=

Also: SVN Location for Android BookWorm project

Upvotes: 0

delformo
delformo

Reputation: 1001

Have you tried running "adb shell bmgr run" at the command prompt?

That will force the Backup manager to do its work.

It seem that dataChanged() schedules the work. But the work does not get done (waits forever) unless forced as described above.

Upvotes: 2

steve
steve

Reputation: 825

I'm having this same issue. If I run "adb shell bmgr run" the backup does occur successfully, however, I can't get dataChanged() to do anything? I have everything configured correctly, because the backup runs via the adb command; does it really just take forever to schedule? I copied my apk to my phone and dataChanged() did nothing there as well. I had hoped to be able to report back to the user that the backup was successful, but this seems impossible to do since the scheduling of this is hidden (if indeed that's what is happening).

Upvotes: 0

Pete Houston
Pete Houston

Reputation: 15089

Just a sample you may wanna try:

http://www.edumobile.org/android/android-development/backup-manager/

EDIT: sorry, I mis-read your XML, you can try to remove this tag on Manifest

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

@p/s: If you can share code, it may help a little.

Upvotes: 0

Caner
Caner

Reputation: 59258

Could this be the problem?:

Only the Backup Manager can call your backup agent's onBackup() method. Each time that your application data changes and you want to perform a backup, you must request a backup operation by calling dataChanged() (see Requesting Backup for more information). A backup request does not result in an immediate call to your onBackup() method. Instead, the Backup Manager waits for an appropriate time, then performs backup for all applications that have requested a backup since the last backup was performed.

http://developer.android.com/guide/topics/data/backup.html#BackupAgent

Upvotes: 1

Related Questions