如何向活动添加计时器并将其值转发到下一个活动?
我的ShuffleButtons.class
中有一个计时器但是当我转到下一个活动ShuffleButtons1.class
时,计时器值从零开始。有人可以告诉我如何将计时器值传递给下一个活动吗?
以下是我的代码:
import java.util.ArrayList;
import java.util.Collections;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ShuffleButtons extends Activity {
int id = 1;
static Toast button_press;
int a = 0;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.shuffle );
timerValue = (TextView) findViewById(R.id.timerValue);
button_press= Toast.makeText(this, "1 Presssed", Toast.LENGTH_SHORT);
//create another two linear layouts which will host 5 buttons horizontally
LinearLayout top_compte = (LinearLayout)findViewById(R.id.top_compte);
LinearLayout bottom_calculator = (LinearLayout)findViewById(R.id.bottom_calculator);
// Create an ArrayList to hold the Button objects that we will create
ArrayList<Button> buttonList = new ArrayList<Button>();
// Create the Buttons, set their text as numeral value of the index variable
for (int i = 0; i < 4; i++) {
final Button b = new Button(this);
b.setText("" + (i+1));
b.setGravity(Gravity.CENTER_HORIZONTAL);
b.setId(i+1); // Set an id to Button
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dealWithButtonClick(b);
}
public void dealWithButtonClick(Button b) {
switch(b.getId()) {
case 1:
if (a==0) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
a=1;
}
button_press.setText("1 Pressed");
button_press.show();
break;
case 2:
if (a==1) {
a=2;
button_press.setText("2 Pressed");
button_press.show();
}else {
button_press.setText("You Pressed the wrong button");
button_press.show();
}
break;
case 3:
if (a==2) {
a=3;
button_press.setText("3 Pressed");
button_press.show();
}else {
button_press.setText("You Pressed the wrong button");
button_press.show();
}
break;
case 4:
if (a==3) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
a=4;
button_press.setText("Good Going");
button_press.show();
// here i move on from ShuffleButtons.class to ShuffleButtons1.class
Intent intent = new Intent(getApplicationContext(),ShuffleButtons1.class);
startActivity(intent);
finish();
}else {
button_press.setText("You Pressed the wrong button");
button_press.show();
}
break;
}
}
});
buttonList.add(b);
}
// Shuffle
Collections.shuffle(buttonList);
for (int i = 0; i < 4; i++) {
// Add the first five Buttons to top_compte
// Add the last five Buttons to bottom_calculator
if (i < 2) {
top_compte.addView(buttonList.get(i));
} else {
bottom_calculator.addView(buttonList.get(i));
}
}
}
// Generates and returns a valid id that's not in use
public int generateUniqueId(){
View v = findViewById(id);
while (v != null){
v = findViewById(++id);
}
return id++;
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}
答案 0 :(得分:0)
不要通过你的计时器。
在单独的服务中启动计时器,这意味着让它在后台运行。
使用Singleton模式或自定义Application类使全局访问计时器。
然后随时随地访问它。
这是更聪明的方法。如果您不熟悉Android服务组件,请参阅this。