2011-08-19 59 views
0

我有需要備份數據並恢復相同的類。我已經提到了api演示中給出的例子。但它根本不起作用。無法調用BackupAgentHelper的備份()方法

有人可以幫我解決這個問題嗎?

的Android的Manifest.xml

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


    <application android:icon="@drawable/icon" android:label="@string/app_name" android:backupAgent="MyBackupAgent" android:restoreAnyVersion="true" android:allowBackup="true" android:enabled="true"> 
     <activity android:name=".BackupServiceDemo" 
        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="AEdPqrEAAAAIAjALiYV5vv5cRGD5L649XByQMnFFYfApskNIfg" /> 

    </application> 
</manifest> 

MyActivity.class

public class BackupRestoreActivity extends Activity implements OnClickListener { 

BackupManager mBackupManager; 

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

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

} 
} 

MyBackupAgent.class

public class MyBackupAgent extends BackupAgentHelper { 


    static final String MY_PREFS_BACKUP_KEY = "AEdPqrEAAAAIAjALiYV5vv5cRGD5L649XByQMnFFYfApskNIfg"; 
    static final String APP_DATA_KEY = "mydata"; 
    String TAG = this.getClass().getSimpleName(); 
    @Override 
    public void onCreate() { 

    Log.i("MyBackupAgent >>>>>> ","into Oncreate() of my Backup Agent >>>>"); 


    } 

    @Override 
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,ParcelFileDescriptor newState) throws IOException { 

      Log.i(TAG, "I m onBackup"); 
      ByteArrayOutputStream bufStream = new ByteArrayOutputStream(); 
      DataOutputStream outWriter = new DataOutputStream(bufStream); 
      outWriter.writeUTF("Hello Backup Service!"); 

      byte[] buffer = bufStream.toByteArray(); 
      int len = buffer.length; 
      data.writeEntityHeader(APP_DATA_KEY, len); 
      data.writeEntityData(buffer, len); 

      FileOutputStream outstream = new FileOutputStream(newState.getFileDescriptor()); 
      DataOutputStream out = new DataOutputStream(outstream); 
      out.writeUTF("Hello Backup Service!"); 
      Log.i(TAG, "Backup Message Completed"); 

    } 

    @Override 
    public void onRestore(BackupDataInput data, int appVersionCode,ParcelFileDescriptor newState) throws IOException { 
     Log.i(TAG, "I m onRestore"); 
     while (data.readNextHeader()) { 
       String key = data.getKey(); 
       int dataSize = data.getDataSize(); 
       if(key.equals(APP_DATA_KEY)){ 
        byte[] dataBuf = new byte[dataSize]; 
        data.readEntityData(dataBuf, 0, dataSize); 
        ByteArrayInputStream baStream = new ByteArrayInputStream(dataBuf); 
        DataInputStream in = new DataInputStream(baStream); 
        String read = in.readUTF(); 
        Log.i(TAG, "Restored Message :" + read); 
       } 
       else{ 
        data.skipEntityData(); 
       } 

     } 
    } 
} 
+0

你應該包括你的AndroidManifest.xml文件和你的BackupAgentHelper的代碼。 – Femi

+0

請參考代碼,並幫我出來.....謝謝 – Richa

回答

0

看那docs:如果您在onCreate然後擴展BackupAgentHelper你必須註冊一個或多個BackupHelperinstances來執行實際工作。如果你不這樣做,我假設框架會忽略你的班級,而不是簡單地調用onBackuponRestore

編輯:你需要執行一個BackupHelper,然後在你的BackupAgentHelperonCreate方法你需要調用addHelper

+0

我沒有得到你可以üPLZ告訴我需要在我的文件中做出的變化..... ???? – Richa

+0

我已經添加了兩行bt它仍然不會調用類:MyBackupAgent \t FileBackupHelper fileHelper = new FileBackupHelper(getBaseContext(),「Myfile」); addHelper(FILES_BACKUP_KEY,fileHelper); – Richa

0

如果要備份完整文件(從SharedPreferences或內部存儲),則應使用BackupAgentHelper構建備份代理。 對於要添加到您的BackupAgentHelper每個幫助,你必須做你的onCreate()方法中的以下內容:

  1. 實例化所需的輔助類的實例。在類構造函數中,您必須指定要備份的相應文件。
  2. 調用addHelper()將助手添加到您的BackupAgentHelper。

樣品:

public class MyPrefsBackupAgent extends BackupAgentHelper { 
    // The name of the SharedPreferences file 
    static final String PREFS = "user_preferences"; 

    // A key to uniquely identify the set of backup data 
    static final String PREFS_BACKUP_KEY = "prefs"; 

    // Allocate a helper and add it to the backup agent 
    @Override 
    public void onCreate() { 
     SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS); 
     addHelper(PREFS_BACKUP_KEY, helper); 
    } 
}