保存在应用程序数据 [英] Saving data in application

查看:124
本文介绍了保存在应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提出了申请。这是一个按钮,showes你有pressed它的时间。每次我杀的应用程序,计时器从0开始再次(自然)。如何使应用程序保存按钮pressed的时间,所以当应用的新被杀害,然后你打开它时,计时器就是在那个时候,你stopped.I有红色的一些关于如何做到这一点,和我认为它具有财产以后做共享preferences。

我的code:

 公共类MainActivity扩展ActionBarActivity {

    按钮按钮1;
    天文台染色体;
    保护时间长;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        染色体=(天文台)findViewById(R.id.chromo);

        button1.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                // TODO自动生成方法存根
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                    chromo.setBase(SystemClock.elapsedRealtime()+时间);
                    chromo.start();
                }
                否则如果(event.getAction()== MotionEvent.ACTION_UP){
                    时间= chromo.getBase() -  SystemClock.elapsedRealtime();
                    chromo.stop();
                }
                返回true;
            }
    });
}}
 

解决方案

编辑:

 公共类MainActivity扩展ActionBarActivity {

    按钮按钮1;
    天文台染色体;
    保护时间长= 0;
    私人共享preferences preFS;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        染色体=(天文台)findViewById(R.id.chromo);
        preFS = getShared preferences(preFS,Context.MODE_PRIVATE);
        长savedValue的= prefs.getLong(my_chrono,0);

        如果(savedValue的== 0)
            chromo.setBase(SystemClock.elapsedRealtime());
        其他
            chromo.setBase(SystemClock.elapsedRealtime()+ savedValue的);

        button1.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                // TODO自动生成方法存根
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                    chromo.start();
                }
                否则如果(event.getAction()== MotionEvent.ACTION_UP){
                    时间= chromo.getBase() -  SystemClock.elapsedRealtime();
                    chromo.stop();
                    prefs.edit()putLong(my_chrono,时间)。适用()。
                }
                返回true;
            }
    });
}}
 

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

要使用共享preferences,初始化这个在您的onCreate

 共享preferences preFS = getShared preferences(the_package_of_your_app,Context.MODE_PRIVATE);
 

然后,尝试获得保存的值

  INT my_saved_value = prefs.getInt(the_package_of_your_app.my_int_1,0);
如果(my_saved_value!= 0)
    //你的定时器的值保存,做什么需要它
其他
    //没有值保存,或定时器是在0
 

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

  prefs.edit()putInt(the_package_of_your_app.my_int_1,my_value)。适用();
 

I have made an application. It's a button that showes the time you have pressed it. Every time i "kill" the application, the timer starts at 0 again (naturally). How can I make the application save the time the button is pressed, so when the appliction is killed, and then you open it, the timer is at that time you stopped.I have red some about how this is done, and i think it has somthing to do with SharedPreferences.

My code:

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

解决方案

EDIT :

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

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

To use the shared preferences, initialize this in you onCreate

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

Then, try to get the saved value

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

Now you have to save that value when needed (when the timer is stopped, or the application is closed)

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

这篇关于保存在应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆