0
例如:在Activty
AI有Chronometer
,我點擊播放開始它,當它到達00:00:10我決定去Activity
B.在返回到A之前,我會繼續等待30秒。我會發現計時器暫停仍顯示00:00:10,但我想看到它仍在運行,然後在00:00:40。我怎樣才能做到這一點?我如何避免在Activity
的變化中暫停計時器?不要停止計時器,當我改變活動
例如:在Activty
AI有Chronometer
,我點擊播放開始它,當它到達00:00:10我決定去Activity
B.在返回到A之前,我會繼續等待30秒。我會發現計時器暫停仍顯示00:00:10,但我想看到它仍在運行,然後在00:00:40。我怎樣才能做到這一點?我如何避免在Activity
的變化中暫停計時器?不要停止計時器,當我改變活動
(根據針對Get time of chronometer widget給出,其中代碼,並加入額外的活性的答案)
主要活動:
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);
}
};
}
的其他活動,切換到:
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();
}
};
}
最後清單:
<?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>
我不確定這是個好主意,但爲什麼不使用'Thread'? – skamlet 2012-03-30 14:43:10