gpt4 book ai didi

java - 当我更改 Activity 时不要停止计时器

转载 作者:行者123 更新时间:2023-12-01 15:27:56 24 4
gpt4 key购买 nike

例如:在 Activty A 中,我有一个 Chronometer,我单击“播放”来启动它,当它到达 00:00:10 时,我决定去Activity B. 我在上面停留 30 秒,然后返回 A。我会发现计时器暂停后仍显示 00:00:10,但我希望看到它仍在运行,然后在 00 :00:40。我怎样才能做到这一点?如何避免计时器在 Activity 更改时暂停?

最佳答案

(基于 Get time of chronometer widget 给出的答案,添加了代码和额外的 Activity 。)

主要 Activity :

package com.so.chilledrat.chronoexample;

import android.app.Activity;
import android.content.Intent;
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;
import android.widget.Toast;

public class ChronoExampleActivity 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);

Button switchButton = new Button(this);
switchButton.setText("Switch Activity");
switchButton.setOnClickListener(mSwitchListener);
layout.addView(switchButton);

setContentView(layout);
}

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

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

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

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

View.OnClickListener mSwitchListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(ChronoExampleActivity.this.getBaseContext(), OtherActivity.class);
startActivityForResult(myIntent, 0);
}
};
}

要切换到的其他 Activity :

package com.so.chilledrat.chronoexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

Button switchButton = new Button(this);
switchButton.setText("Switch Back");
switchButton.setOnClickListener(mSwitchListener);
layout.addView(switchButton);

setContentView(layout);
}

View.OnClickListener mSwitchListener = new OnClickListener() {

@Override
public void onClick(View v) {
// Intent myIntent = new Intent(OtherActivity.this.getBaseContext(),
// ChronoExampleActivity.class);
// startActivityForResult(myIntent, 0);
finish();
}

};
}

最后是 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.so.chilledrat.chronoexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ChronoExampleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="OtherActivity"></activity>
</application>

</manifest>

关于java - 当我更改 Activity 时不要停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945089/

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