Last active
December 16, 2015 20:49
-
-
Save zaki50/5495394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.backupagentsample" | |
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:backupAgent="com.example.backupagentsample.MyBackupAgent" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<meta-data android:name="com.google.android.backup.api_key" | |
android:value="AEdPqrEAAAAIg-APWubuFTxLp5DLMzGQnBNifZdlOJcB9TxK7A" /> | |
<activity | |
android:name="com.example.backupagentsample.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> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.backupagentsample; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.app.backup.BackupManager; | |
import android.view.Menu; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
new BackupManager(this).dataChanged(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.backupagentsample; | |
import android.app.backup.BackupAgent; | |
import android.app.backup.BackupDataInput; | |
import android.app.backup.BackupDataOutput; | |
import android.os.ParcelFileDescriptor; | |
import android.util.Log; | |
import java.io.IOException; | |
public class MyBackupAgent extends BackupAgent { | |
@Override | |
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, | |
ParcelFileDescriptor newState) throws IOException { | |
Log.d("hoge", "begin backup"); | |
data.writeEntityHeader("hoge", 10); | |
data.writeEntityData(new byte[] {0,1,2,3,4,5,6,7,8,9}, 10); | |
Log.d("hoge", "end backup"); | |
} | |
@Override | |
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) | |
throws IOException { | |
Log.d("hoge", "begin restore"); | |
if (data.readNextHeader()) { | |
final String key = data.getKey(); | |
final int size = data.getDataSize(); | |
final byte[] buffer = new byte[size]; | |
data.readEntityData(buffer, 0, size); | |
Log.d("hoge", "buffer[3]: " + buffer[3]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment