gpt4 book ai didi

Android 保持秒表运行

转载 作者:行者123 更新时间:2023-11-29 14:15:57 26 4
gpt4 key购买 nike

我正在开发一个倒数计时器。但我希望它即使在我回到之前的 Activity 时也能运行。我怎么能做到这一点?这里是 Android 新手。

这是我到目前为止所做的: 私有(private)按钮暂停、停止、开始、重置; 私有(private)计时码表; 长时间停止 = 0;

@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

最佳答案

您希望您的应用继续运行,即使 Activity 不再可见。这个问题已经有了答案。例如这里:How to keep an Android app running indefinitely?

关于Android 保持秒表运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12988735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com