2013-04-02 24 views
0

我想在我的整個應用程序中擁有一個我希望具有常量(或常見)的​​特定重要事項(我從服務器每隔15秒提取一次)的列表。所以當我通過Intents(或任何其他方法)轉向下一個活動時,我應該始終擁有該列表。在android中可能嗎?如何保持我的應用程序的某些部分在整個應用程序中通用

我想要不同的解決方案,儘可能減少工作量。 請幫忙..

編輯:我想我沒有讓自己清楚。我不擔心如何存儲數據。我在問怎麼才能實現只有一半屏幕變化(當我們從活動轉移到活動時)而另一半保持不變(不移動)的視圖。可以嗎?

回答

0

如果數據量很大,可以使用Shared Preferences或SQLite DB存儲數據。如果數據量較少,則可以使用靜態變量。如果你使用靜態變量,當應用中發生數據可能丟失的任何崩潰。因此,靜態變量的使用不太可取。

+0

@ Kameswari-我取數據從服務器每15秒......所以我不看好數據太多擔心......但它不應該獲取數據的每時間新的活動被提取! –

0

有很多方法可以做到這一點:

  1. 您可以保存它們,並在您的應用程序的SQLite數據庫 見SQLite的(除非你刪除的應用程序或執行應用程序代碼刪除數據是持續)使用方法here
  2. 您可以使用將它們緩存在手機內存中,直到您的應用運行。這裏 請參見緩存使用here
1

您的應用程序類的實例總是從任何活動入店。

所有你需要做的就是創建應用程序類是這樣的:

public class YourApp extends Application { 
.... 
} 

然後修改下面一行在你的應用程序的AndroidManifest.xml:

<application 
    android:name="your.package.YourApp" 

現在您可以訪問這個類無處不在:

YourApp appInstance = (YourApp)getApplication(); 
0
  1. 您可以使用SQLite數據庫來存儲這些數據,然後創建單例助手來讀取它。

  2. 或者您可以將您的數據以XMLJSON格式保存爲文件,然後解析它們以讀取。

  3. 或者,您可以爲您的數據的一個實體創建類的容器,使之序列化和SharedPreferences存儲爲ArrayList<YourDataContainer>

1

使用PreferencesManager類似下面,創建你的POJO訪問PreferencesManager 。

// TODO: Auto-generated Javadoc 
/** 
* The Class PreferenceManager. 
*/ 
public class PreferenceManager { 

    /** The Constant TAG. */ 
    private static final String TAG = PreferenceManager.class.getSimpleName(); 

    /** The default shared preferences. */ 
    private static SharedPreferences defaultSharedPreferences = null; 

    /** 
    * Inits the. 
    * 
    * @param context the context 
    */ 
    public static final void init(Context context){ 
     defaultSharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context); 
     log("Initialize PreferenceManager!"); 
     UserSettings.init(context); 
    } 

    /** 
    * Save. 
    * 
    * @param name the name 
    * @param value the value 
    */ 
    static final void save(String name,String value){ 
     if(value != null){ 
      Editor edit = defaultSharedPreferences.edit(); 
      edit.remove(name); 
      edit.putString(name, value); 
      edit.commit(); 
     }else{ 
      Editor edit = defaultSharedPreferences.edit(); 
      edit.remove(name); 
      edit.commit(); 
     } 
    } 

    /** 
    * Gets the. 
    * 
    * @param name the name 
    * @param defaultValue the default value 
    * @return the string 
    */ 
    public static final String get(String name,String defaultValue){ 
     return defaultSharedPreferences.getString(name, defaultValue); 
    } 

    /** 
    * Save state. 
    * 
    * @param name the name 
    * @param state the state 
    */ 
    public static final void saveState(String name,Bundle state){ 
     if(state != null && state.size() > 0){ 
      Parcel parcel = Parcel.obtain(); 
      parcel.writeBundle(state); 
      String encodeToString = Base64.encodeToString(parcel.marshall(), Base64.DEFAULT); 
      PreferenceManager.save(name, encodeToString); 
     }else{ 
      PreferenceManager.save(name, null); 
     } 
     log("Saved state "+name); 
    } 

    /** 
    * Gets the state. 
    * 
    * @param name the name 
    * @return the state 
    */ 
    public static final Bundle getState(String name){ 
     log("Get state "+name); 
     String encryptedValue = ""; 
     try { 
      encryptedValue = PreferenceManager.get(name, ""); 
     } catch (NullPointerException e) { 
      return new Bundle(); 
     } 
     if("".equals(encryptedValue)){ 
      return new Bundle(); 
     }else{ 
      byte[] decode = Base64.decode(encryptedValue, Base64.DEFAULT); 
      Parcel parcel = Parcel.obtain(); 
      parcel.unmarshall(decode, 0, decode.length); 
      parcel.setDataPosition(0); 
      return parcel.readBundle(); 
     } 
    } 

    /** 
    * Log. 
    * 
    * @param msg the msg 
    */ 
    private static final void log(String msg){ 
     Log.d(TAG, msg); 
    } 

} 



/** 
* The Class Settings. 
*/ 
public class UserSettings { 

    /** The settings bundle. */ 
    private final Bundle settingsBundle = new Bundle(1); 

     /** 
    * Save. 
    */ 
    public final void save() { 
     PreferenceManager.saveState(SETTINGS_STATE_NAME, settingsBundle); 
    } 

    /** 
    * Restore. 
    */ 
    final public void restore() { 
     settingsBundle.clear(); 
     Bundle state = PreferenceManager.getState(SETTINGS_STATE_NAME); 
     if (state.size() == 0) { 
      settingsBundle.putAll(getDefaultValuesSettings()); 
     } else { 
      settingsBundle.putAll(state); 
     } 
    } 

    final void reset() { 
     settingsBundle.clear(); 
    } 


    /** 
    * Gets the settings. 
    * 
    * @return the settings 
    */ 
    public static UserSettings getSettings() { 
     return settings; 
    } 

    /** 
    * Inits the. 
    * 
    * @param ctx the ctx 
    */ 
    public static final void init(Context ctx) { 
     settings.restore(); 
     setDeviceUniqueId(ctx, settings); 
    } 

} 

用法示例:

public class YourApplication extends Application { 
.... 
    onCreate(){ 
    .... 
    PreferenceManager.init(getBaseContext()); 
    }         
} 

Where you need your data to be stored and retrieved use the methods like below. 
UserSettings.getSettings().setUser(responseVal); 
UserSettings.getSettings().save(); 

String response = UserSettings.getSettings().getUser(); 
+0

你看到我的編輯了嗎? –

+0

那麼在這種情況下,你不需要緩存數據。您可以使用IntentService來下載數據。請參閱此項目https://github.com/commonsguy/cwac-wakeful。有關IntentService的更多信息,請查看http://developer.android.com/reference/android/app/IntentService.html –

+0

@ LalithB - 我對數據不感興趣..我對視圖感興趣! –

相關問題