2014-03-06 17 views
1

我感興趣的節省從他們共享偏好和加載數據我的數據。我想知道正確的方式來做到這一點:類和數據,對象,應用程序的Android生命週期,廣播接收器

我認爲情況是清楚的Activity類,Android提供了的onCreate和中的onDestroy我們應該保存和負荷首選項。我希望我沒有錯:)

但是,如果我有一個從Object派生類的一些像這樣的

public class LoggerSingleton { 
    // Constants 
    private static final String LOG_FILE = "log"; 
    private static final Integer MAX_LINES = 200; 

    // Variables 
    private static List<String> logsList; 
    private static Context appContext; 

    // class initialization is called first time you call getInstance 
    private static LoggerSingleton instance = new LoggerSingleton(); 

    public static LoggerSingleton getInstance() { 
     return instance; 
    } 

    // Constructor 
    private LoggerSingleton() { 
     logsList = new ArrayList<String>(); 
     appContext = MyApplication.getContext(); 

     SharedPreferences settings = appContext.getSharedPreferences(LOG_FILE, Context.MODE_PRIVATE); 
     try { 
      JSONArray jsonArray = new JSONArray(settings.getString(LOG_FILE, "[]")); 
      for (int i = 0; i < jsonArray.length(); i++) 
       logsList.add(jsonArray.getString(i)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static String getLogs() { 
     return logsList.toString(); 
    } 

    public static void appendLog(String newLog) { 
     String currentDateandTime = Utils.getMyDateTime(); 

     newLog = currentDateandTime + "_" + newLog; 
     Log.d(appContext.getString(appContext.getApplicationInfo().labelRes), newLog); 
     logsList.add(newLog); 

     while (logsList.size() > MAX_LINES) 
      logsList.remove(0); 

     JSONArray jsonArray = new JSONArray(); 
     for (int i = 0; i < logsList.size(); i++)   
      jsonArray.put(logsList.get(i)); 

     SharedPreferences settings = appContext.getSharedPreferences(LOG_FILE, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putString(LOG_FILE, jsonArray.toString()); 
     editor.commit(); 
    } 

} 

我打電話的getInstance()MyApplication的構造函數中。


我的問題是,什麼時候創建了我的對象,何時被銷燬?如何捕捉摧毀事件?如何正確保存我的字符串列表到共享的前綴?

我要覆蓋所有的情況:當OS破壞不使用很長一段時間我的對象,當用戶不「強制停止」,並重新啓動應用程序,當手機進入關機和背部。

我想我知道發生了什麼活動,但類會發生什麼情況是這樣,它的數據嗎?從應用程序派生類,當它被破壞,會發生什麼,從它的數據丟失?與從BroadcastReceiver派生的類相同。是否有一些將所有這些類的數據保存爲首選項的一般方法?

回答

2
public class MyApplication extends Application { 

@Override 
public void onCreate() { 
    super.onCreate(); 
    LoggerSingleton.initLoggerSingleton(getApplicationContext()); 
} 

}

初始化應用的單例類(應用初始化開始) 你初始化你的對象YourApplication(擴展應用) 後,你可以在你的應用程序 如果從任何活動訪問應用程序是由上巢OS殺死它開始將重新初始化在類的單

小的變化

public class LoggerSingleton { 
// Constants 
private static final String LOG_FILE = "log"; 
private static final Integer MAX_LINES = 200; 

// Variables 
private List<String> logsList; 
private Context appContext; 

// class initialization is called first time you call getInstance 
private static LoggerSingleton instance ; 

public static LoggerSingleton getInstance() { 
    return instance; 
} 
public static void initLoggerSingleton(Context context){ 
    if(instance==null) 
     instance = new LoggerSingleton(context); 
} 

// Constructor 
private LoggerSingleton(Context context) { 
    logsList = new ArrayList<String>(); 
    appContext = context; 

    SharedPreferences settings = appContext.getSharedPreferences(LOG_FILE, Context.MODE_PRIVATE); 
    try { 
     JSONArray jsonArray = new JSONArray(settings.getString(LOG_FILE, "[]")); 
     for (int i = 0; i < jsonArray.length(); i++) 
      logsList.add(jsonArray.getString(i)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public String getLogs() { 
    return logsList.toString(); 
} 

public void appendLog(String newLog) { 
    String currentDateandTime = Utils.getMyDateTime(); 

    newLog = currentDateandTime + "_" + newLog; 
    Log.d(appContext.getString(appContext.getApplicationInfo().labelRes), newLog); 
    logsList.add(newLog); 

    while (logsList.size() > MAX_LINES) 
     logsList.remove(0); 

    JSONArray jsonArray = new JSONArray(); 
    for (int i = 0; i < logsList.size(); i++)   
     jsonArray.put(logsList.get(i)); 

    SharedPreferences settings = appContext.getSharedPreferences(LOG_FILE, Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putString(LOG_FILE, jsonArray.toString()); 
    editor.commit(); 
} 

}

後,您可以從活動或不同類

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    LoggerSingleton.getInstance().appendLog("String"); 



} 

}

,以節省您的活動電話的onStop數據和讀取在onStart /的onResume

最好的方式訪問單

當活動調用onDestory方法os市場您的應用程序將從進程中刪除,並從操作系統中刪除,如果操作系統需要更多的momory

約節省您的ArrayList中sharedPref Saving Serializable Objects List into sharedPreferences

+0

謝謝你的答案。我對這種情況感興趣:我的活動被破壞了,我的廣播接收器獲取了一些數據,並且需要將它們存儲在某個地方。因爲用戶可以強制停止應用程序,然後再打開它。你在這種情況下推薦什麼? – Milan

0

你可以嘗試做這樣 如果不知道它要做到這一點,而不是測試正確的方式,

public class IntentReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    LoggerSingleton.initLoggerSingleton(context); 
    LoggerSingleton.getInstance().appendLog("String"); 

} 

}

BrodcatsReceiver有一個方法onReceive他在哪裏獲得上下文嘗試初始化您的單身人士並保存您的數據

PS不知道是否正確的方法來保存從廣播接收器恢復的日期從不測試它嘗試讀取BroadcastReceiver