-3

I make two activity first is timer.java activity which is an countdown activity and second activity is SaveRestTime.java in this activity user input number value in edittext and user will save it i use shared preference in this activity now I want which value user save in SaveRestTime.java class is automatically make value of countdown if user not save any value then default value is 30 sec please help me i am confuse how to read value from another activity here is timer.java code

package com.cinegoes.www.daily10exercise;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import com.cinegoes.www.daily10exercise.SaveRestTime;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

import static com.cinegoes.www.daily10exercise.SaveRestTime.mypreference;

/**
 * Created by ever on 7/25/2017.
 */

public class timer extends Activity implements View.OnClickListener {

private CountDownTimer countDownTimer;
private boolean timerStarted = false;
private Button buttonStart;
public TextView textView;
private ProgressBar progressBarCircle;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timer);
    buttonStart = (Button) this.findViewById(R.id.button);
    progressBarCircle = (ProgressBar) findViewById(R.id.progressBarCircle);
    buttonStart.setOnClickListener(this);
    textView = (TextView) this.findViewById(R.id.textView);
    countDownTimer = new CountDownTimerActivity(startTime, interval);
    textView.setText(textView.getText() + String.valueOf(startTime/1000));
}

@Override
protected void onStart() {
    super.onStart();
    countDownTimer.start();
    timerStarted = true;
    setProgressBarValues();
}


@Override
public void onClick(View v) {
    finish();
}


public class CountDownTimerActivity extends CountDownTimer {
    public CountDownTimerActivity(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        textView.setText(msTimeFormatter(startTime));
        textView.setText("Time's up!");
        setProgressBarValues();
        finish();
    }


    @Override
    public void onTick(long millisUntilFinished) {
        textView.setText(msTimeFormatter(millisUntilFinished));

        progressBarCircle.setProgress((int) (millisUntilFinished / 1000));
    }
}

private void setProgressBarValues() {

    progressBarCircle.setMax((int) startTime / 1000);
    progressBarCircle.setProgress((int) startTime / 1000);
}
private String msTimeFormatter(long milliSeconds) {

    String ms = String.format("%02d:%02d",
            TimeUnit.MILLISECONDS.toMinutes(milliSeconds) - 
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliSeconds)),
            TimeUnit.MILLISECONDS.toSeconds(milliSeconds) - 
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));

    return ms;


}


}

here is my SaveRestTime.java code

package com.cinegoes.www.daily10exercise;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class SaveRestTime extends Activity {
SharedPreferences sharedpreferences;
TextView name;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";


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

    name = (TextView) findViewById(R.id.etName);
    sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);
    if (sharedpreferences.contains(Name)) {
        name.setText(sharedpreferences.getString(Name, ""));
    }

}

public void Save(View view) {
    String n = name.getText().toString();
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Name, n);
    editor.commit();
}



public void Get(View view) {
    name = (TextView) findViewById(R.id.etName);
    sharedpreferences = getSharedPreferences(mypreference, 
Context.MODE_PRIVATE);

    if (sharedpreferences.contains(Name)) 
{name.setText(sharedpreferences.getString(Name, ""));
    }

}

}
Varinder
  • 21
  • 8
  • Exactly the same way you would read it from any activity. The data saved in the SharedPreferences are not dependent on the Activity. You can get the shared preferences from any component of you application. – zeekhuge Jul 25 '17 at 10:26
  • You saved the shared pref in timer.java using the key Name, now use the "Get" method in SaveRestTime.java to get the pref. Btw you should use intent params in this case – Ezio Jul 25 '17 at 10:26
  • Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Ajil O. Jul 25 '17 at 10:30

1 Answers1

1
SharedPreferences prefs = this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
String lanSettings = prefs.getString("nameKey", null);
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
ManishNegi
  • 569
  • 1
  • 6
  • 19