abh*_*hek 26 backup android sharedpreferences
我想使用扩展BackupAgentHelper的MyBackUpAgent类在Android中备份数据.我正在使用SharedPreferences来存储数据.
我的主要活动代码是:
public class MainActivity extends Activity {
EditText inputtext;
TextView outputtext;
Button submit;
public static SharedPreferences sharedprefs;
static final String File_Name_Of_Prefrences ="godplay_preferences";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
init();
sharedprefs=getSharedPreferences(File_Name_Of_Prefrences,MODE_PRIVATE);
System.out.println("value="+sharedprefs.getString("Input",""));
outputtext.setText(sharedprefs.getString("Input",""));
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
populateUI();
}
});
}
public void populateUI()
{
String savedinput=inputtext.getText().toString();
System.out.println("savedinput="+savedinput);
outputtext.setText(savedinput);
sharedprefs=getSharedPreferences(File_Name_Of_Prefrences,MODE_PRIVATE);
Editor editor=sharedprefs.edit();
editor.putString("Input",inputtext.getText().toString());
editor.commit();
requestBackup();
}
private void init() throws ClassCastException
{
inputtext=(EditText) findViewById(R.id.edtInputText);
outputtext=(TextView) findViewById(R.id.txtOutputText);
submit=(Button) findViewById(R.id.btnSubmit);
}
public void requestBackup() {
BackupManager bm = new BackupManager(getApplicationContext());
bm.dataChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
我的MyBackUpAgent类:
public class MyBackUpAgent extends BackupAgentHelper{
static final String PREFS_BACKUP_KEY = "backup";
String key_string="Hello World";
@Override
public void onCreate() {
System.out.println("********************");
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this,MainActivity.File_Name_Of_Prefrences);
addHelper(PREFS_BACKUP_KEY, helper);
}
}
Run Code Online (Sandbox Code Playgroud)
我的mainfest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.godplay"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:restoreAnyVersion="false"
android:backupAgent=".MyBackUpAgent"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.godplay.MainActivity"
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="AEdPqrEAAAAIhjloadYCTPUNo3yPsSX6LKmziiumZiQVlEEdBA" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已尝试使用bmgr工具进行测试,它正在使用bmgr工具成功执行.但是,在Android设备和模拟器上进行测试时,备份不会发生,也不会恢复.
此外,我已经在Android 5.1,Android 4.2和Android 4.0上对此进行了测试,但仍然没有运气.
在我看来,我的MyBackUpAgent类永远不会被调用,我在MyBackUpAgent类中尝试过断点并验证它.它永远不会受到打击.
我究竟做错了什么?
在您的 AndroidManifest.xml 文件中,尝试更改
android:backupAgent=".MyBackUpAgent"
Run Code Online (Sandbox Code Playgroud)
具有完全限定的类名,即
android:backupAgent="com.abh.utils.MyBackUpAgent"
Run Code Online (Sandbox Code Playgroud)
但当然可以将“com.abh.utils”更改为 MyBackUpAgent.java 所在的包的名称。
| 归档时间: |
|
| 查看次数: |
671 次 |
| 最近记录: |