android如何使用SharedPreference编程秒表

时间:2013-11-11 05:38:33

标签: java android

我正在安装android sdk中的秒表,但即使我按回来,我也想继续前进。我在某种程度上理解了活动生命周期,我应该过度使用onPause和onResume方法但不知道如何使用它,因为SharePreference编辑器不能接受Chronometer对象

    package com.example.taekwondobuddy.util;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.LinearLayout;



 public class Time extends Activity {
 Chronometer mChronometer;

@Override
  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

mChronometer = new Chronometer(this);
layout.addView(mChronometer);

Button startButton = new Button(this);
startButton.setText("Start");
startButton.setOnClickListener(mStartListener);
layout.addView(startButton);

Button stopButton = new Button(this);
stopButton.setText("Stop");
stopButton.setOnClickListener(mStopListener);
layout.addView(stopButton);

Button resetButton = new Button(this);
resetButton.setText("Reset");
resetButton.setOnClickListener(mResetListener);
layout.addView(resetButton);        

setContentView(layout);
}



View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
    mChronometer.start();

}
};

View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
    mChronometer.stop();

}
};

View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
    mChronometer.setBase(SystemClock.elapsedRealtime());

 }
 };
 }

任何提示或建议?感谢帮助!

2 个答案:

答案 0 :(得分:2)

// try this
public class Time extends Activity {
    Chronometer mChronometer;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        mChronometer = new Chronometer(this);

        layout.addView(mChronometer);

        Button startButton = new Button(this);
        startButton.setText("Start");
        startButton.setOnClickListener(mStartListener);
        layout.addView(startButton);

        Button stopButton = new Button(this);
        stopButton.setText("Stop");
        stopButton.setOnClickListener(mStopListener);
        layout.addView(stopButton);

        Button resetButton = new Button(this);
        resetButton.setText("Reset");
        resetButton.setOnClickListener(mResetListener);
        layout.addView(resetButton);

        setContentView(layout);
    }

    private void showElapsedTime() {
        long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
        Toast.makeText(MyActivity.this, "Elapsed milliseconds: " + elapsedMillis,
                Toast.LENGTH_SHORT).show();
    }

    View.OnClickListener mStartListener = new View.OnClickListener() {
        public void onClick(View v) {
            int stoppedMilliseconds = 0;

            String chronoText = mChronometer.getText().toString();
            String array[] = chronoText.split(":");
            if (array.length == 2) {
                stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
                        + Integer.parseInt(array[1]) * 1000;
            } else if (array.length == 3) {
                stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
                        + Integer.parseInt(array[1]) * 60 * 1000
                        + Integer.parseInt(array[2]) * 1000;
            }

            mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
            mChronometer.start();
        }
    };

    View.OnClickListener mStopListener = new View.OnClickListener() {
        public void onClick(View v) {
            mChronometer.stop();
        }
    };

    View.OnClickListener mResetListener = new View.OnClickListener() {
        public void onClick(View v) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        prepareSharedData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mChronometer.setText(sharedPreferences.getString("time", "00:00"));
    }

    @Override
    protected void onPause() {
        super.onPause();
        prepareSharedData();
    }

    public void prepareSharedData(){
        String chronoText = mChronometer.getText().toString();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("time", chronoText);
        editor.commit();
    }
}

答案 1 :(得分:0)

如果你想继续倒计时你离开它而不是你需要共享偏好,如果你想继续观看,即使用户按下后退按钮你也不需要共享偏好,共享偏好用于存储基元和字符串数据类型,您可以使用AsyncTaskService来执行此操作。如果您使用计时器,则无法将Chronometer本身存储在共享首选项中,而是将其值返回elapsedRealtime()Chronometer的其他方法 在AsyncTask或Service中,您可以保持轮询elapsedRealtime()AsyncTask保持活动状态,直到其doInBackground方法完成,即使您按下后退按钮AsyncTask保持活动