gpt4 book ai didi

android - BackupAgentHelper 从未在 Android 2.2 中调用

转载 作者:太空狗 更新时间:2023-10-29 16:22:08 25 4
gpt4 key购买 nike

我正尝试在我的应用程序中实现数据备份。我构建了 Android 2.2 项目,并在 Galaxy s2 4.0.3 中运行。

我尝试使用:BackupManagerTest将首选项保存到云端

这是我的代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.amdroid.backuptest"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />

<application
android:allowBackup="true"
android:backupAgent="net.amdroid.backuptest.MyBackupAgent"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".BackupManagerTestActivity"
android:label="@string/app_name" >
<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="AEdPqrEAAAAI7_yf1xqlpltWZPZiKMHVlDgn3nMfgotjUweSUg" />
</application>

</manifest>

MyBackupAgent.java

public class MyBackupAgent extends BackupAgentHelper {
// The names of the SharedPreferences groups that the application maintains. These
// are the same strings that are passed to getSharedPreferences(String, int).
static final String PREFS_TEST = "testprefs";

// An arbitrary string used within the BackupAgentHelper implementation to
// identify the SharedPreferenceBackupHelper's data.
static final String MY_PREFS_BACKUP_KEY = "myprefs";

// Simply allocate a helper and install it
@Override
public void onCreate() {
SharedPreferencesBackupHelper helper =
new SharedPreferencesBackupHelper(this, PREFS_TEST);
addHelper(MY_PREFS_BACKUP_KEY, helper);
Log.d("Test", "Adding backupagent...");
}
}

我的 Activity

public class BackupManagerTestActivity extends Activity {

private SharedPreferences prefs;
private Editor edit;
private BackupManager backupManager;
private EditText text;

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

backupManager = new BackupManager(getBaseContext());
prefs = getSharedPreferences(MyBackupAgent.PREFS_TEST, Context.MODE_PRIVATE);
edit = prefs.edit();

text = (EditText) findViewById(R.id.editName);
String nome = prefs.getString("KEY_NAME", "");
text.setText(nome);

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

public void onClick(View v) {
edit.putString("KEY_NAME", text.getText().toString());
edit.commit();
Log.d("Test", "Calling backup...");
backupManager.dataChanged();
}
});
}
}

所以 MyBackupAgent 从未调用过。我不知道原因。

最佳答案

当您调用 backupManager.dataChanged() 时,它只是安排您的应用程序进行备份。这并不意味着您的后备助手会立即被调用。

来自 http://developer.android.com/guide/topics/data/backup.html :

You can request a backup operation at any time by calling dataChanged(). This method notifies the Backup Manager that you'd like to backup your data using your backup agent. The Backup Manager then calls your backup agent's onBackup() method at an opportune time in the future. Typically, you should request a backup each time your data changes (such as when the user changes an application preference that you'd like to back up). If you call dataChanged() several times consecutively, before the Backup Manager requests a backup from your agent, your agent still receives just one call to onBackup().

Note: While developing your application, you can request a backup and initiate an immediate backup operation with the bmgr tool.

bmgr 工具的说明可在以下位置找到: http://developer.android.com/tools/help/bmgr.html

要强制所有挂起的备份操作立即运行,请使用:

adb shell bmgr run

关于android - BackupAgentHelper 从未在 Android 2.2 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11810235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com