2015-01-15 86 views
1

我的意圖是爲我的應用程序實施備份服務,在進行更改時存儲在雲配置屬性中,並在應用程序首次啓動時檢索它們。使用Android備份服務備份我的SharedPreferences

但我有一些問題:備份過程不正確。

我的Android清單包含以下內容:

<application 
    ... 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:allowBackup="true" 
    android:backupAgent=".backup.BizkaimoveBackup"> 
     ... 

     <!-- Android Backup --> 
     <meta-data 
      android:name="com.google.android.backup.api_key" 
      android:value="--key--" /> 
     <!-- Android Backup --> 

的BackupAgentHelper類包含:

public class BizkaimoveBackup extends BackupAgentHelper { 

    /* 
    * Atributos 
    */ 
    private static String LOG_TAG = "BizkaimoveBackup"; 
    private SharedPreferencesBackupHelper spbh; 

    /** 
    * El nombre del fichero de SharedPreferences: el mismo que hay en globales. 
    */ 
    private static String PREFS = "bizkaimovePrefs"; 

    /** 
    * Una clave para identificar unequívocamente un conjunto de datos de backup 
    */ 
    private static final String PREFS_BACKUP_KEY = "myprefs"; 

    /* 
    * Métodos 
    */ 
    @Override 
    public void onCreate() { 
     spbh = new SharedPreferencesBackupHelper(this, PREFS); 
     addHelper(PREFS, spbh); 
     Log.d(LOG_TAG, "Añadiendo BackupAgent..."); 
    } 

} 

要存儲在雲中的變化,我做了以下內容:

switchPreferenceIdiomaEs = (SwitchPreference) findPreference("pref_switch_idioma_es"); 
    switchPreferenceIdiomaEu = (SwitchPreference) findPreference("pref_switch_idioma_eu"); 

    switchPreferenceIdiomaEs.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 

     @Override 
     public boolean onPreferenceChange(Preference preference, Object newValue) { 
      //newValue es boolean aquí 
      SharedPreferences sp = getActivity().getSharedPreferences("bizkaimovePrefs", Context.MODE_PRIVATE); 
      Editor spEditor = sp.edit(); 
      spEditor.putBoolean("idiomaEsCastellano", (Boolean) newValue); 
      spEditor.putBoolean("idiomaEsEuskera", !((Boolean) newValue)); 
      spEditor.commit(); 
      switchPreferenceIdiomaEu.setChecked(!((Boolean) newValue)); 

      BackupManager.dataChanged("com.ingartek.bizkaimove"); 

      return true; 
     } 
    }); 

的XML preferencias.xml文件包含以下內容:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:key="bizkaimovePrefs" 
    android:persistent="true" 
    android:id="@+id/listaPrefs"> 

    <PreferenceCategory 
     android:title="@string/config_idioma"> 

     <!-- Un switch para euskera, otro para castellano --> 
     <SwitchPreference 
      android:key="pref_switch_idioma_eu" 
      android:title="@string/config_euskera" 
      android:switchTextOn="@string/config_euskera" 
      android:switchTextOff="@string/config_castellano"/> 

     <SwitchPreference 
      android:key="pref_switch_idioma_es" 
      android:title="@string/config_castellano" 
      android:switchTextOn="@string/config_castellano" 
      android:switchTextOff="@string/config_euskera"/> 

    </PreferenceCategory> 
    ... 

最後,內部的onCreate()的HomeActivity我請執行下列操作:

BackupManager bm = new BackupManager(this); 
     bm.requestRestore(new RestoreObserver() { 

      @Override 
      public void restoreStarting(int numPackages) { 
       Toast.makeText(getApplicationContext(), "Empezando recuperación de backup...", Toast.LENGTH_SHORT).show(); 
       super.restoreStarting(numPackages); 
      } 

      @Override 
      public void restoreFinished(int error) { 
       Toast.makeText(getApplicationContext(), "Recuperación de backup finalizada. Error: " + error, Toast.LENGTH_SHORT).show(); 
       super.restoreFinished(error); 

然而,在這裏做http://developer.android.com/guide/topics/data/backup.html#Testing描述的動作後沒有數據被卸載我的應用程序後取出。

我已經能夠下載並測試這個https://bitbucket.org/andreaskristensson/android-example-preference-fragment-and-backup-api/downloads但它都不起作用。

我在做什麼不正確?

在此先感謝。

+0

從你的日誌語句你能夠看到你的BackupAgent是否被調用來執行備份?針對本地備份傳輸進行測試要比測試Google傳輸容易得多。 –

+0

我該怎麼做,@HayesHaugen? – russellhoff

+0

看這裏http://developer.android.com/tools/help/bmgr.html – dVaffection

回答

1

最後我意識到我的問題是...

我創造了另一個文件和一些喜好都存儲在那裏,其餘的被保存默認的首XML文件中。

所以備份管理器只與其中一個人合作。