带暂停和恢复的Android倒数计时器不工作?
我是android新手,正在制作一个应用程序,其中有一个带有暂停和恢复切换按钮的倒计时计时器。当我暂停计时器时,一切正常。它暂停了,但当我恢复时,它没有从暂停的地方恢复,而是从后台运行的实际计时器恢复 我做了很多研发,但没有得到解决方案。请有人帮帮我,谢谢 这是我的密码:带暂停和恢复的Android倒数计时器不工作?,android,countdowntimer,Android,Countdowntimer,我是android新手,正在制作一个应用程序,其中有一个带有暂停和恢复切换按钮的倒计时计时器。当我暂停计时器时,一切正常。它暂停了,但当我恢复时,它没有从暂停的地方恢复,而是从后台运行的实际计时器恢复 我做了很多研发,但没有得到解决方案。请有人帮帮我,谢谢 这是我的密码: public class QuizActivityB extends Activity { ToggleButton pauseTB; private String setQuestion, testNa
public class QuizActivityB extends Activity {
ToggleButton pauseTB;
private String setQuestion, testName, time, timecounter;
private String timeCounterFinish;
private static double passedTime;
private static Handler timeHandler = new Handler();
private static double timeStart = SystemClock.uptimeMillis();;
private TextView timeLabel;
private static boolean initialStartTime = false;
public double testTime;
private double remaining;
private Runnable updateTask = new Runnable() {
public void run() {
double duration = 60 * 1000 * testTime;
double now = SystemClock.uptimeMillis();
remaining = duration - passedTime - (now - timeStart);
passedTime = now - timeStart + passedTime;
timeStart = now;
if (passedTime > 0) {
int seconds2 = (int) (passedTime / 1000);
int minutes2 = seconds2 / 60;
seconds2 = seconds2 % 60;
if (seconds2 < 10) {
timeCounterFinish = "" + minutes2 + ":0" + seconds2;
} else {
timeCounterFinish = "" + minutes2 + ":" + seconds2;
}
}
if (remaining > 0) {
int seconds = (int) (remaining / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (seconds < 10) {
timeLabel.setText("Time Left: " + "" + minutes + ":0"
+ seconds);
} else {
timeLabel.setText("Time Left: " + "" + minutes + ":"
+ seconds);
}
timeHandler.postDelayed(this, 1000);
} else {
timeHandler.removeCallbacks(this);
initialStartTime = false;
passedTime = 0;
timeStart = SystemClock.uptimeMillis();
PostFinishedData();
Intent intent = new Intent(QuizActivityB.this,
TestAnalysisActivity.class);
intent.putExtra("userID", testID);
intent.putExtra("time", time);
intent.putExtra("currentDateandTime", date);
intent.putExtra("score", finalScore);
intent.putExtra("finalPercentage", finalPercentage);
intent.putExtra("totalAttemp", setAttemptedQuestion);
intent.putExtra("totalQuestions",
String.valueOf(totalQuestions));
intent.putExtra("Wrong", WrongAnswers);
intent.putExtra("Correct", correctQ);
intent.putExtra("timecounter", timeCounterFinish);
startActivity(intent);
finish();
}
}
};
pauseTB = (ToggleButton) findViewById(R.id.pauseBtIV);
pauseTB.setBackgroundResource(R.drawable.pausebutton);
finishBT.setVisibility(View.GONE);
testName = getIntent().getStringExtra("testName");
testTime = Integer.parseInt(getIntent().getStringExtra("time"));
timeLabel = (TextView) findViewById(R.id.timeDetailsTV);
timeLabel.setVisibility(View.VISIBLE);
if (!initialStartTime) {
timeStart = SystemClock.uptimeMillis();
initialStartTime = true;
}
timeHandler.post(updateTask);
pauseTB.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
pauseTB.setBackgroundResource(R.drawable.resumebutton);
String timecounter = String.valueOf(passedTime);
PausePrefEditor.putString("pauseTime", timecounter);
PausePrefEditor.commit();
timeHandler.removeCallbacks(updateTask);
timeHandler = null;
passedTime = 0;
initialStartTime = false;
timeStart = SystemClock.uptimeMillis();
} else {
// showToastResume();
String time = pausePreference.getString("pauseTime", "");
if (time.length() > 0) {
double timeSaved = Double.valueOf(time);
passedTime = timeSaved;
timeHandler = new Handler();
timeHandler.post(updateTask);
}
}
公共类QuizActivityB扩展活动{
切换按钮暂停B;
私有字符串setQuestion,testName,time,timecounter;
私人字符串反义;
私有静态双通道时间;
私有静态处理程序timeHandler=新处理程序();
专用静态双timeStart=SystemClock.uptimeMillis();;
私有文本视图时间标签;
私有静态布尔initialStartTime=false;
公共双测试时间;
私人双剩余;
private Runnable updateTask=new Runnable(){
公开募捐{
双倍持续时间=60*1000*testTime;
double now=SystemClock.uptimeMillis();
剩余时间=持续时间-passedTime-(现在-timeStart);
passedTime=now-timeStart+passedTime;
timeStart=now;
如果(passedTime>0){
int seconds2=(int)(passedTime/1000);
整数分钟2=秒2/60;
seconds2=seconds2%60;
如果(秒2<10){
timeCounterFinish=“”+分钟2+”:0“+秒2;
}否则{
timeCounterFinish=“”+分钟2+”:“+秒2;
}
}
如果(剩余>0){
整数秒=(整数)(剩余/1000);
整数分钟=秒/60;
秒=秒%60;
如果(秒<10){
timeLabel.setText(“剩余时间:+”+分钟+“:0”
+秒);
}否则{
timeLabel.setText(“剩余时间:+”+分钟+:”
+秒);
}
timeHandler.postDelayed(这个,1000);
}否则{
timeHandler.removeCallbacks(这个);
initialStartTime=false;
passedTime=0;
timeStart=SystemClock.uptimeMillis();
后完成数据();
意向意向=新意向(QuizActivityB.this,
TestAnalysisActivity.class);
intent.putExtra(“userID”,testID);
意图。额外(“时间”,时间);
intent.putExtra(“当前日期和时间”,日期);
意图。putExtra(“分数”,最终分数);
意向。额外(“最终百分比”,最终百分比);
intent.putExtra(“totalAttemp”,setAttemptedQuestion);
intent.putExtra(“totalQuestions”,
String.valueOf(totalQuestions));
意图。putExtra(“错误”,错误答案);
意图。putExtra(“正确”,correctQ);
意图。putExtra(“计时器”,timecounter,timeCounterFinish);
星触觉(意向);
完成();
}
}
};
pauseTB=(ToggleButton)findviewbyd(R.id.pauseBtIV);
pauseTB.setBackgroundResource(R.drawable.pausebutton);
finishBT.setVisibility(View.GONE);
testName=getIntent().getStringExtra(“testName”);
testTime=Integer.parseInt(getIntent().getStringExtra(“time”);
timeLabel=(TextView)findViewById(R.id.timeDetailsTV);
timeLabel.setVisibility(View.VISIBLE);
如果(!initialStartTime){
timeStart=SystemClock.uptimeMillis();
initialStartTime=true;
}
timeHandler.post(updateTask);
setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,
布尔值(已检查){
如果(已检查){
pauseTB.setBackgroundResource(R.drawable.resumebutton);
String timecounter=String.valueOf(passedTime);
putString(“pauseTime”,timecounter);
PausePrefEditor.commit();
removeCallbacks(updateTask);
timeHandler=null;
passedTime=0;
initialStartTime=false;
timeStart=SystemClock.uptimeMillis();
}否则{
//showtostresume();
String time=pausePreference.getString(“pauseTime”,”);
if(time.length()>0){
double timeSaved=double.valueOf(时间);
passedTime=节省的时间;
timeHandler=newhandler();
timeHandler.post(updateTask);
}
}
尝试使用此代码。它将帮助您
Timer.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Timer extends Activity {
Button b1,b2,b3;
private TextView t1;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
SimpleDateFormat pp;
String chec;
String count;
String name="Timer";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer);
t1=(TextView)findViewById(R.id.timerValue);
b1=(Button)findViewById(R.id.startButton);
b2=(Button)findViewById(R.id.pauseButton);
b3=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
}
public void reset(View v)
{
customHandler.removeCallbacks(updateTimerThread);
// startTime = 0L;
timeSwapBuff=0L;
t1.setText("00:00:00");
}
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);
t1.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}
timer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pauseButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="37dp"
android:textSize="40sp"
android:textColor="#ffffff"
android:text="00:00:00" />
<Button
android:id="@+id/startButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="18dp"
android:text="Start" />
<Button
android:id="@+id/pauseButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignTop="@+id/startButton"
android:layout_centerHorizontal="true"
android:text="Pause" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/pauseButton"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/pauseButton"
android:text="Reset"
android:onClick="reset"/>
</RelativeLayout>
尝试使用计时器您能帮我写这段代码吗,就像我在许多活动中使用这段代码一样。一切正常,但“恢复”按钮不能从后台计时器运行其恢复,但不能从暂停的位置运行。请帮我。您能帮我解决我的代码问题吗?因为一切正常,但“恢复”按钮不能恢复时间r暂停的位置。