【问题标题】:stopwatch with shared preference-android具有共享偏好的秒表-android
【发布时间】:2017-09-27 05:55:36
【问题描述】:

我正在开发我的第一个应用程序,即秒表

所以使用下面的代码,我可以创建一个秒表,它工作正常。但是此时,当我完全关闭应用程序并重新启动它时,它的统计信息从 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,那么即使在完全关闭应用程序后,用户应该在打开时获得最后一个计时器值再次应用,

【问题讨论】:

  • 请说明您是如何尝试使用 SharedPreferences 的。

标签: android stopwatch


【解决方案1】:
    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);

试试这个..

【讨论】:

  • 我已经尝试过了,它成功地给了我最后一个时间值,但是当我按下开始按钮时,它不是从最后一个值开始,而是从 0:0:0 开始
【解决方案2】:
     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);
    }

【讨论】:

    【解决方案3】:

    我得到了答案, 实际上我错过了 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();
        }
    };
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2017-05-17
      • 2019-06-20
      • 1970-01-01
      • 2012-10-24
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多