1

system.currentTimeMillis を使用して stopWatch の開始時刻を記録するにはどうすればよいですか? activity onResume()またはが呼び出されたときにonStart()、時計を更新して、保存された時刻からの経過時間を表示するにはどうすればよいですか? ありがとう。私がこれまで試したことはありませんonResume()

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.stopwatch);
    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.clock32);

    // initializes controls
    initControls();

    // sets format for chronometer
    chronometer.setText( "00:00:00" );


}


private void initControls(){

    // initializes buttons & chronometer
    pause = ( Button ) findViewById ( R.id.btStopWatchPause );
    stop = ( Button ) findViewById ( R.id.btStopWatchStop );
    start = ( Button) findViewById ( R.id.btStopWatchStart );
    reset = ( Button ) findViewById ( R.id.btStopWatchReset );
    chronometer = ( Chronometer ) findViewById ( R.id.chronometer );

    // sets listeners for buttons and chronometer
    pause.setOnClickListener(this);
    stop.setOnClickListener(this);
    start.setOnClickListener(this);
    reset.setOnClickListener(this);
    chronometer.setOnChronometerTickListener(this);


}


public void onClick(View v) {
    // TODO Auto-generated method stub

    switch( v.getId() )
    {
    case R.id.btStopWatchStart:
        chronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
        chronometer.start();
        break;
    case R.id.btStopWatchStop:
        chronometer.stop();
        break;
    case R.id.btStopWatchReset:
        chronometer.setBase(SystemClock.elapsedRealtime());
        break;
    case R.id.btStopWatchPause:
        timeWhenStopped = chronometer.getBase() - SystemClock.elapsedRealtime();
        chronometer.stop();
        break;

    } // end of switch statement

}

public void onChronometerTick(Chronometer c) {
    // TODO Auto-generated method stub

    CharSequence text = c.getText();
    if ( text.length()  == 5 ) {
        c.setText( "00:" + text );
    } else if ( text.length() == 7 ) {
        c.setText( "0" + text );
    }



} // end of onChronometerTick method
4

1 に答える 1

0
    @Override
    protected void onResume() {
    super.onResume();
    TimeForActivity.StartTimer(AccountingTab.this);

    public class TimeForActivity implements Constants{
    public static void StartTimer(Context context, TextView aTvTime, Button aBtUserStatus, ProgressDialog aProgressDialog, String sessionID) {

        mHandler.removeCallbacks(mUpdateTimeTask);

        activity = context;
        tvTime = aTvTime;       
        btUserStatus = aBtUserStatus;

        tvTime.setText("");
        btUserStatus.setText(statusItems[userStatus - 1]);


        btUserStatus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                showStatusDialog();
            }
        });
        progressDialog = aProgressDialog;       

        reStartTimer();
    }
    private static void reStartTimer() {


    if (pageConfig.getUserStatusTime() != null && StatusService.methodType != StatusService.GO_OUT) {

            if (mStartTime == null)
                mStartTime = pageConfig.getUserStatusTime() * 60 * 1000 - SystemClock.elapsedRealtime();
            //CommonHelper.showToastMsg(activity, Long.toString(SystemClock.elapsedRealtime()));
            //tvUserStatus.setText(Long.toString(SystemClock.uptimeMillis()) + " mStarTime: " + mStartTime);
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);
        }
    }

private static Runnable mUpdateTimeTask = new Runnable() {

        public void run() {

            try {
                final long start = mStartTime;          
                long millis = SystemClock.elapsedRealtime() + start;
                long seconds = (long) (millis / 1000);
                long hours =  seconds / 3600;
                seconds -= hours * 3600;
                long minutes = seconds / 60;
                seconds     = seconds % 60;


                tvTime.setText(CommonHelper.getTwoDigitTime(hours) +  ":" + CommonHelper.getTwoDigitTime(minutes)); 
                        //+ ":" + CommonHelper.getTwoDigitTime(seconds));
                //java.lang.System.out.println(tvTime.getText().toString());
                if (userStatus == StatusService.GO_OUT){
                   tvTime.setText("");
                   stopTimer();
                }
                if (StatusService.methodType != StatusService.GO_OUT)
                    mHandler.postAtTime(this, SystemClock.elapsedRealtime() + 60*1000);

            } catch (Exception e) {
                // TODO: handle exception
            }

        }
    };
    }
于 2012-10-22T15:53:03.657 に答える