Saving data in application

后端 未结 4 1924
梦如初夏
梦如初夏 2021-01-24 00:14

I have made an application. It\'s a button that shows the time you have pressed it. Every time I \"kill\" the application, the timer starts at 0 again (naturally). How can I mak

4条回答
  •  灰色年华
    2021-01-24 00:31

    Saving in SharedPreferences :

    SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
    // We use an editor to insert values in SharedPreferences
    Editor editor = prefs.edit(); 
    // Saving the values
    editor.putLong("myTime", time); 
    // Committing the changes
    editor.commit(); 
    

    Retrieving saved values :

    long savedValue = 0l;
    SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
    
    if (prefs.contains("hello")){
        savedValue = sharedpreferences.getLong("myTime", 0l));
    }
    

提交回复
热议问题