D
D
Dmitry Bobreshov2019-08-07 01:16:23
Java
Dmitry Bobreshov, 2019-08-07 01:16:23

How to save value in Sharedpreferences?

There is a code below.
I can’t figure out how to save the timer in Sharedpreferences so that when the application closes, it continues to count down?
And, if it's not difficult, tell me how to make the timer be displayed as 00:00, otherwise I have thousands there =(
I do it in Android Studio

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import java.io.IOException;


public class MainActivity<time> extends AppCompatActivity {
    //Объявим переменные компонентов
    Button button;
    TextView textView;
    TextView mTimer;
    SharedPreferences nTime;




    //Переменная для работы с БД
    private BDGuru mDBHelper;
    private SQLiteDatabase mDb;

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


        mDBHelper = new BDGuru(this);

        try {
            mDBHelper.updateDataBase();
        } catch (IOException mIOException) {
            throw new Error("UnableToUpdateDatabase");
        }

        try {
            mDb = mDBHelper.getWritableDatabase();
        } catch (SQLException mSQLException) {
            throw mSQLException;
        }

        //Найдем компоненты в XML разметке
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);
        mTimer = (TextView) findViewById(R.id.mTimer);

        //Клик по кнопке
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                String product = "";
                Cursor cursor = mDb.rawQuery("SELECT * FROM Guru ORDER BY RANDOM() LIMIT 1;", null);
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {
                    product = cursor.getString(1);
                    cursor.moveToNext();
                }
                cursor.close();

                textView.setText(product);
                v.setVisibility(View.GONE);

                int _12HoursInMilSecs = 12 * 60 * 60 * 1000;
                v.postDelayed(new Runnable() {
                    public void run() {
                        v.setVisibility(View.VISIBLE);
                    }
                }, _12HoursInMilSecs);


                new CountDownTimer(_12HoursInMilSecs,1000) {

                    public void onTick(long millisUntilFinished) {
                        mTimer.setText("seconds remaining: " + millisUntilFinished / 1000);


                    }

                    public void onFinish() {
                        mTimer.setText("");
                    }

                }
                .start();


            }
        });

    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question