我正在开发我的第一个应用程序,即秒表
所以使用下面的代码,我可以创建一个秒表,它的工作正常。但是在这一点上,当我完全关闭应用程序并再次启动时,它的统计数据从0:0:0我知道我应该使用共享偏好,但我对此没有足够的了解,所以我不是得到我想要的东西。
public class StopwatchFragment extends Fragment implements View.OnClickListener {
public static StopwatchFragment newInstance() {
StopwatchFragment fragment = new StopwatchFragment();
return fragment;
}
FloatingActionButton stopwatchButton;
ImageView stopwatchReplayButton, stopwatchShareButton;
TextView timerValue;
private long startTime=0L;
long timeInMilliSec= 0L;
long timeSwapBuff=0L;
long updatedTime=0L;
int secs, mins, millisec;
private Handler customHandler= new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
stopwatchButton=(FloatingActionButton) view.findViewById(R.id.stopwatchButton);
stopwatchButton.setOnClickListener(this);
timerValue=(TextView) view.findViewById(R.id.timerValue);
stopwatchReplayButton=(ImageView)view.findViewById(R.id.stopwatchReplayButton);
stopwatchReplayButton.setOnClickListener(this);
stopwatchShareButton=(ImageView)view.findViewById(R.id.stopwatchShareButton);
stopwatchShareButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
if(view==stopwatchButton)
{
if(stopwatchButton.getTag().toString().equals("off"))
{
stopwatchButton.setTag("on");
stopwatchButton.setImageResource(R.drawable.pause);
startTime= SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);
stopwatchReplayButton.setVisibility(View.VISIBLE);
}
else
{
stopwatchButton.setTag("off");
stopwatchShareButton.setVisibility(View.VISIBLE);
stopwatchButton.setImageResource(R.drawable.play);
timeSwapBuff+=timeInMilliSec;
customHandler.removeCallbacks(updateTimerThread);
}
}
if(view==stopwatchReplayButton)
{
customHandler.removeCallbacks(updateTimerThread);
timerValue.setText("0 : 00 : 00");
stopwatchButton.setTag("off");
stopwatchButton.setImageResource(R.drawable.play);
timeInMilliSec= 0L;
timeSwapBuff=0L;
updatedTime=0L;
}
if(view==stopwatchShareButton)
{
String text="My Time is "+mins+" : "+secs+" : "+millisec;
Intent i=new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,text);
startActivity(Intent.createChooser(i,"Share Using"));
}
}
private Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
timeInMilliSec = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliSec;
secs = (int) (updatedTime / 1000);
mins = secs / 60;
secs = secs % 60;
millisec = (int) (updatedTime % 1000);
timerValue.setText("" + mins + " : " + String.format("%02d", secs) + " : " + String.format("%03d", millisec));
customHandler.postDelayed(this, 0);
}
};
}
我只想实现:如果用户按下停止按钮停止秒表,并假设计时器值为0:3:50,那么即使在完全关闭应用程序后,用户也应该在打开时获取最后一个计时器值该应用程序,
答案 0 :(得分:0)
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("SNOW_DENSITY",mSnowDensity);
editor.commit();
//To retrieve
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0);
试试这个..
答案 1 :(得分:0)
final static int READ=1;
final static int WRITE=2;
private List<HashMap<String, String>> ListMaps=
new ArrayList<HashMap<String, String>>();
private String FileName="CData";
private String F_Path="";
}
F_Path=FileName;
Log.d("F_Path", " File Path :"+F_Path);
load_data();
public void Add_Data(){
Log.d("Add", "Add Data Calling");
HashMap<String, String> map= new HashMap<String, String>();
map.put("Name",d_name_et.getText().toString() );
ListMaps.add(map);
d_name_et.setText("");
Disp_Data();
}
public void Save_Data(){
Log.d("Save", "Save Data Calling");
try {
writeListToFile((ArrayList<HashMap<String, String>>) ListMaps);
} catch (Exception e) {
Log.d("Save", "writeListToFile call error");
}
}
public void Disp_Data(){
Log.d("Disp_Data()", "Disp_Data() Calling--->");
String CurData="";
for (int i=0; i < ListMaps.size();i++){
HashMap<String, String> map=ListMaps.get(i);
CurData =CurData+map.get("Name")+"\n";
}
dip_tv.setText(CurData);
}
答案 2 :(得分:0)
我得到了答案, 实际上我错过了timeSwapBuff的概念:
所以这里是秒表的更正代码
public class StopwatchFragment extends Fragment implements View.OnClickListener {
public static StopwatchFragment newInstance() {
StopwatchFragment fragment = new StopwatchFragment();
return fragment;
}
FloatingActionButton stopwatchButton;
ImageView stopwatchReplayButton, stopwatchShareButton;
TextView timerValue;
private long startTime=0L;
long timeInMilliSec= 0L;
long timeSwapBuff=0L;
long updatedTime=0L;
int secs, mins, millisec;
public SharedPreferences pref;
public static SharedPreferences.Editor editor;
private Handler customHandler= new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
stopwatchButton=(FloatingActionButton) view.findViewById(R.id.stopwatchButton);
stopwatchButton.setOnClickListener(this);
timerValue=(TextView) view.findViewById(R.id.timerValue);
pref=this.getActivity().getSharedPreferences("stopwatch", Context.MODE_PRIVATE);
editor=pref.edit();
timerValue.setText("" + pref.getInt("Key1",0) + " : " + String.format("%02d", pref.getInt("Key2",0)) + " : " + String.format("%03d", pref.getInt("Key3",3)));
int a=pref.getInt("Key1",0);
int b=pref.getInt("Key2",0);
int c=pref.getInt("Key3",0);
Time t=Time.valueOf(a +":"+b+":"+c);
int mm=a*60000;
mm=mm+b*1000;
mm=mm+c;
timeSwapBuff= new Long(mm);
stopwatchReplayButton=(ImageView)view.findViewById(R.id.stopwatchReplayButton);
stopwatchReplayButton.setOnClickListener(this);
stopwatchShareButton=(ImageView)view.findViewById(R.id.stopwatchShareButton);
stopwatchShareButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
if(view==stopwatchButton)
{
if(stopwatchButton.getTag().toString().equals("off"))
{
stopwatchButton.setTag("on");
stopwatchButton.setImageResource(R.drawable.pause);
startTime= SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);
stopwatchReplayButton.setVisibility(View.VISIBLE);
}
else
{
stopwatchButton.setTag("off");
stopwatchShareButton.setVisibility(View.VISIBLE);
stopwatchButton.setImageResource(R.drawable.play);
timeSwapBuff+=timeInMilliSec;
customHandler.removeCallbacks(updateTimerThread);
}
}
if(view==stopwatchReplayButton)
{
customHandler.removeCallbacks(updateTimerThread);
timerValue.setText("0 : 00 : 00");
stopwatchButton.setTag("off");
stopwatchButton.setImageResource(R.drawable.play);
timeInMilliSec= 0L;
timeSwapBuff=0L;
updatedTime=0L;
}
if(view==stopwatchShareButton)
{
String text="My Time is "+mins+" : "+secs+" : "+millisec;
Intent i=new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,text);
startActivity(Intent.createChooser(i,"Share Using"));
}
}
private Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
timeInMilliSec = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliSec;
secs = (int) (updatedTime / 1000);
mins = secs / 60;
secs = secs % 60;
millisec = (int) (updatedTime % 1000);
timerValue.setText("" + mins + " : " + String.format("%02d", secs) + " : " + String.format("%03d", millisec));
editor.putInt("Key1",mins);
editor.putInt("Key2",secs);
editor.putInt("Key3",millisec);
customHandler.postDelayed(this, 0);
editor.commit();
}
};
}