在应用程序中保存数据

我已经申请了。 它是一个按钮,显示您按下它的时间。 每当我“杀死”应用程序时,计时器再次从0开始(自然)。 如何使应用程序保存按下按钮的时间,所以当应用程序被杀死然后你打开它时,计时器就在你停止的时候。我有一些关于如何完成这个的问题,我认为它与SharedPreferences有关。

我的代码:

public class MainActivity extends ActionBarActivity { Button button1; Chronometer chromo; protected long time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.button1); chromo=(Chronometer)findViewById(R.id.chromo); button1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_DOWN){ chromo.setBase(SystemClock.elapsedRealtime()+time); chromo.start(); } else if( event.getAction() == MotionEvent.ACTION_UP){ time =chromo.getBase()-SystemClock.elapsedRealtime(); chromo.stop(); } return true; } }); }} 

保存在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(); 

检索已保存的值:

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

在偏好中设置值:

 SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("name", "slama"); editor.putInt("idName", 28); editor.commit(); 

从首选项中检索数据:

 // MY_PREFS_NAME - a static String variable like: //public static final String MY_PREFS_NAME = "MyPrefsFile"; SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("text", null); if (restoredText != null) { String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. String name = prefs.getString("name", ""); int idName = prefs.getInt("idName", 0); //0 is the default value. } 

这是一个Android共享首选项教程: http : //www.tutorialspoint.com/android/android_shared_preferences.htm

它可以帮助您解决问题

编辑:

 public class MainActivity extends ActionBarActivity { Button button1; Chronometer chromo; protected long time = 0; private SharedPreferences prefs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.button1); chromo=(Chronometer)findViewById(R.id.chromo); prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE); long savedValue = prefs.getLong("my_chrono", 0); if(savedValue == 0) chromo.setBase(SystemClock.elapsedRealtime()); else chromo.setBase(SystemClock.elapsedRealtime() + savedValue); button1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_DOWN){ chromo.start(); } else if( event.getAction() == MotionEvent.ACTION_UP){ time =chromo.getBase()-SystemClock.elapsedRealtime(); chromo.stop(); prefs.edit().putLong("my_chrono", time).apply(); } return true; } }); }} 

================================================== ==========================

要使用共享首选项,请在onCreate中初始化

 SharedPreferences prefs = getSharedPreferences("the_package_of_your_app", Context.MODE_PRIVATE); 

然后,尝试获取保存的值

 int my_saved_value = prefs.getInt("the_package_of_your_app.my_int_1", 0); if(my_saved_value != 0) //your value of your timer was saved, do what's needed with it else //there was no value saved, or the timer was at 0 

现在你必须在需要时保存该值(当计时器停止或应用程序关闭时)

 prefs.edit().putInt("the_package_of_your_app.my_int_1", my_value).apply(); 

详细说明@ 2Dee的答案:

 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(); 

可以进入

 protected void onDestroy(); 

方法。 此方法可以在要调用的Activity中重载,因为活动被销毁(终止,关闭等),以便可以保存任何数据(这是您想要做的)。

同样,

 SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE); time = sharedpreferences.getLong("myTime", 0l); 

可以进入

 protected void onCreate(Bundle savedInstanceState); 

方法。 首次创建活动时会调用此方法。 这会将您的时间设置为保存的值(如果没有则默认为0)。

如果出于某种原因需要在不同的时间调用它们(例如活动生命周期的后期或早期),您可以在此处阅读更多相关信息。

如果你喜欢这个答案,请同意upvote 2Dee的答案。 一些代码从字面上复制/粘贴。

快乐的编码! 如果您有更多问题,请发表评论。