Android 倒计时暂停

Android 倒计时暂停,android,countdowntimer,Android,Countdowntimer,我的应用程序中有一个倒计时。每当我单击imageview时,它都会暂停10秒,并在10秒后自动启动。我正在使用下面的代码。它不工作!我怎样才能做到这一点 MainActivity.java: package-app.thesis.boogleit; 导入java.util.ArrayList; 导入java.util.concurrent.TimeUnit; 导入android.annotation.SuppressLint; 导入android.annotation.TargetApi;

我的应用程序中有一个倒计时。每当我单击imageview时,它都会暂停10秒,并在10秒后自动启动。我正在使用下面的代码。它不工作!我怎样才能做到这一点

MainActivity.java:

package-app.thesis.boogleit;
导入java.util.ArrayList;
导入java.util.concurrent.TimeUnit;
导入android.annotation.SuppressLint;
导入android.annotation.TargetApi;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
@TargetApi(Build.VERSION\u code.gingerbrand)@SuppressLint(“NewApi”)
公共类MainActivity扩展了活动{
//艾尔的变量
DBAdapter-dbHelper;
编辑文字、分数、搜索;
ImageView显示\u addWord;
文本视图分数,tv2,tv3;
//可爱的游戏
受保护的静态最终字符串标记=null;
字符串generatedString=“”;
自动完成文本视图文本;
文本视图定时器;
图像视图搜索词;
ImageView暴雪、炸弹、搜索词、lineone;
ImageButton image1,image2,image3,image4,image5,image6,image7,image8,
图像9、图像10、图像11、图像12、图像13、图像14、图像15、图像16;
列表视图词表;
私人倒计时;
私有布尔值timeHasStarted=false;
@SuppressLint(“CutPasteId”)@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=新的DBAdapter(this);
search=(EditText)findViewById(R.id.textHere);
words=(编辑文本)findviewbyd(R.id.edWord);
分数=(EditText)findViewById(R.id.edScore);
show_addWord=(ImageView)findviewbyd(R.id.show_addWord);
show_addWord.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
//按钮sound.start();
startActivity(新意图(“app.thesis.boggleit.ADDWORD”);
}
});
暴雪=(ImageView)findViewById(R.id.blizzard);
炸弹=(图像视图)findViewById(R.id.bomb);
searchWord=(ImageView)findviewbyd(R.id.searchWord);
wordList=(ListView)findViewById(R.id.wordList);
image1=(ImageButton)findViewById(R.id.Button1);
image2=(ImageButton)findViewById(R.id.Button2);
image3=(ImageButton)findViewById(R.id.Button3);
image4=(ImageButton)findViewById(R.id.Button4);
image5=(ImageButton)findViewById(R.id.Button5);
image6=(ImageButton)findViewById(R.id.Button6);
image7=(ImageButton)findViewById(R.id.Button7);
image8=(ImageButton)findViewById(R.id.Button8);
image9=(ImageButton)findViewById(R.id.Button9);
image10=(ImageButton)findViewById(R.id.Button10);
image11=(ImageButton)findViewById(R.id.Button11);
image12=(ImageButton)findViewById(R.id.Button12);
image13=(ImageButton)findViewById(R.id.Button13);
image14=(ImageButton)findViewById(R.id.Button14);
image15=(ImageButton)findViewById(R.id.Button15);
image16=(ImageButton)findViewById(R.id.Button16);
timer=(TextView)this.findViewById(R.id.timer);
timer.setText(“00:00:30”);
倒计时=新的倒计时(300001000);
最终的AutoCompleteTextView文本=(AutoCompleteTextView)findViewById(R.id.textHere);
最终ImageButton image1=(ImageButton)findViewById(R.id.Button1);
//图像数组
final int[]myPics={};
//随机图像
//自动暂停时间和恢复时间
blizzard.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!timeHasStarted){
倒计时。取消();
timeHasStarted=false;
}
否则{
countDownTimer.start();
timeHasStarted=true;
}
}
});
bomb.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//随机图像
}
});
//单击imgbuttons>在txtview>开始计数计时器中显示文本
OnClickListener mycommocClickListner=新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
Log.i(标记“arg0.getId()=”+arg0.getId());
if(arg0.getId()==R.drawable.a){
Log.i(标记“arg0.getId()=”+arg0.getId());
generatedString=generatedString+“a”;//[PLACEE RESPACTIVE Character在此]
text.setText(generatedString);
如果(!timeHasStarted){
countDownTimer.start();
timeHasStarted=true;
}
}
};
图1.setOnClickListener(MyComMoClickListener);
}
//倒计时
@TargetApi(Build.VERSION\u CODES.GINGERBREAD)@suppressint(“NewApi”)公共类MyCountDownTimer扩展了CountDownTimer{
公共MyCountDownTimer(长毫秒未来,长倒计时间隔){
超级(毫秒未来,倒计时间隔);
}
@TargetApi(Build.VERSION\u code.GINGERBREAD)@SuppressLint(“NewApi”)@Override
公共void onTick(长毫秒未完成){
//TODO自动生成的方法存根
长密耳=完成的密耳;
String hms=String.format(“%02d:%02d:%02d”,时间单位为毫秒到小时(毫秒),
TimeUnit.millides.toMinutes(毫秒)-TimeUnit.HOURS.toMinutes(TimeUnit.millides.toHours(毫秒)),
TimeUnit.millides.toSeconds(毫秒)-TimeUnit.MINUTES.toSe
private static Handler handler = new Handler();
private int totalTimeToRun = 30;//in seconds
private Runnable timerHandler = new Runnable() {
    public int runnableCount = 0;

    @Override
    public void run() {

       handler.removeCallbacks(this);
       //do what the timer is supposed to do when it runs
       if(runnableCount == 30) {
          //30 seconds are up
          //reset
          runnableCount = 0;//so we can reuse it

       } else {    
          //update the time values
          //time left in seconds = 30-runnableCount       
          handler.postDelayed(this, 1000);//every second
       }
       runnableCount++;
    }
}
handler.removeCallbacks(timerHandler);
handler.post(timerHandler);