mercredi 24 février 2016

Android: BackupAgentHelper onCreate not being called

I have implemented SQLite backup using custom BackupHelper. I am calling BackupHelper.dataChanged method. After which, I executed the command:

adb shell bmgr run

But it seems onCreate method is not being called.

AndroidManifest.xml

<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.zubin.finalbackuptest">

<application
    android:backupAgent=".MyBackupAgent"
    android:allowBackup="true">
...
    <meta-data android:name="com.google.android.backup.api_key" android:value="..." />
...
</application>

DBBackupHelper.java

public class DBBackupHelper extends FileBackupHelper {

    public DBBackupHelper(Context context, String database) {
        super(context, context.getDatabasePath(database).getAbsolutePath());
    } 
}

MyBackupAgent.java

public class MyBackupAgent extends BackupAgentHelper {

  static final String DB = "the_ultimate_phone_book_2.db";
  static final String MY_DB_KEY = "database";

  @Override
  public void onCreate() {
      addHelper(MY_DB_KEY, new DBBackupHelper(this, DB));
      Log.d("Test", "Adding backup agent...");
  }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private BackupManager backupManager;
private TextView text;
DatabaseHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Contact c1 = new Contact("first", "middle", "last", "1", "test", "test", 1);
    db = new DatabaseHandler(this);
    backupManager = new BackupManager(this);

    text = (TextView) findViewById(R.id.editName);
    nome = prefs.getString("KEY_NAME", "DEFAULT");

    Button button = (Button) findViewById(R.id.buttonSave);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {           
            db.addContact(c1);
            Log.d("Test", "Calling backup...");
            backupManager.dataChanged();
        }
    });
}

@Override
protected void onResume() {
    super.onResume();        
    List<Contact> list = db.getActiveContacts();
    if (!list.isEmpty())
        text.append("" + list.size());
    else
        text.append("NONE!!");

}
}

Aucun commentaire:

Enregistrer un commentaire