CountDownTimer完成后的第二个活动循环

时间:2014-03-01 04:14:08

标签: android android-intent countdowntimer

我的活动中有一个倒数计时器。

当用户回答纠正时,它将加载带有剩余时间的相同活动。

但倒计时结束时,第二个活动加载循环。

这是我的ArcadeActivty代码:

public class ArcadeActivity extends Activity {

final Context context = this;
ImageView image;
String icon_game;
String[] ar;
String name;
String imge;
String desc;
String clue;
int clue_status=0;
int gid = 0;
int level = 0;
long seconds;
final TestAdapter mDbHelper = new TestAdapter(this);
int coin = 0;
private Button btn_clue;
private Button btn_skip;
private TextView txt_coins;
private View view;
private MediaPlayer mp = null;
private TextView lbl_timer; 
private CountDownTimer countDownTimer;
long currentTime;
int score;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_arcade);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) {
        currentTime = extras.getLong("timeCode");
        score = extras.getInt("myScore");
    } else {
        currentTime = 180000;   
    }

    startTimer();

    final Button btn_submit = (Button) findViewById(R.id.btn_submit);
    btn_clue = (Button) findViewById(R.id.btn_clue);
    btn_skip = (Button) findViewById(R.id.btn_skip);
    lbl_timer = (TextView) findViewById(R.id.lbl_timer);
    final EditText txt_answer = (EditText) findViewById(R.id.txt_answer);


    ImageView img = (ImageView) findViewById(R.id.img_clue);        

    mDbHelper.createDatabase();       
    mDbHelper.open();



    Cursor testdata = mDbHelper.getArcade(); 


    name = Utility.GetColumnValue(testdata, "name");
    imge = Utility.GetColumnValue(testdata, "image");
    clue = Utility.GetColumnValue(testdata, "clue");
    clue_status = Integer.parseInt(Utility.GetColumnValue(testdata, "clue_status"));
    System.out.println(clue_status);
    level = Integer.parseInt(Utility.GetColumnValue(testdata, "level_order"));
    desc = Utility.GetColumnValue(testdata, "description");
    gid = Integer.parseInt(Utility.GetColumnValue(testdata, "_id"));

    int id = getResources().getIdentifier("com.iamnards.gameapp:drawable/" + imge, null, null);
    img.setImageResource(id);

    //lbl_level.setText("Level "+level);

    btn_submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (name.toLowerCase().contains(txt_answer.getText().toString().toLowerCase()) && txt_answer.getText().toString().trim().length() > 0) {
                //playSound(R.raw.complete);    
                Toast.makeText(getApplicationContext(), "Perfect! ", Toast.LENGTH_SHORT).show();
                //load_correct_details(imge,name,desc); 
                mDbHelper.updateArcadeStatus(gid);
                mDbHelper.close();  
                score = score+10;
                Intent i = new Intent(ArcadeActivity.this, ArcadeActivity.class);
                i.putExtra("timeCode", currentTime);
                i.putExtra("myScore", score);
                startActivity(i);

            } else {
                //playSound(R.raw.error);  
                Toast.makeText(getApplicationContext(), "Are you serious?", Toast.LENGTH_SHORT).show();
            }
        }   
    });

    btn_skip.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(ArcadeActivity.this, ArcadeActivity.class);
            i.putExtra("timeCode", currentTime);
            i.putExtra("myScore", score);
            startActivity(i);
        }   
    });

}

void startTimer() {
    countDownTimer = new CountDownTimer(currentTime, 1000) {
        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds) {
            seconds = leftTimeInMilliseconds / 1000;
            currentTime = leftTimeInMilliseconds;
            lbl_timer.setText(String.format("%02d", seconds / 60)
                    + ":" + String.format("%02d", seconds % 60));

        }

        @Override
        public void onFinish() {
            // this function will be called when the timecount is finished
                if(currentTime<1717){
                    Toast.makeText(getApplicationContext(), "TIMES UP! "+currentTime, Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(ArcadeActivity.this, ScoreActivity.class);
                    i.putExtra("myScore", score);
                    startActivity(i);
                    finish();
                }
        }

    }.start();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_arcade, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        mDbHelper.close();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void load_clue(View v){

      //handle the click here
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("Clue:");

        // set dialog message
        alertDialogBuilder
            .setMessage(clue)
            .setCancelable(false)
            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                private TextView txt_coins;

                public void onClick(DialogInterface dialog,int id) {


                }
              });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

  }

}

ScoresActivity代码:

public class ScoreActivity extends Activity {

final TestAdapter mDbHelper = new TestAdapter(this);
private TextView lbl_score; 
int score;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_score);

    Bundle extras = getIntent().getExtras(); 
    score = extras.getInt("myScore");

    lbl_score = (TextView) findViewById(R.id.lbl_score);
    final EditText txt_name = (EditText) findViewById(R.id.txt_name_score);
    final Button btn_save = (Button) findViewById(R.id.btn_save);

    lbl_score.setText("Your score is: "+score);

    mDbHelper.createDatabase();       
    mDbHelper.open();

     btn_save.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {

            if(isEmpty(txt_name)){
                if(isEmpty(txt_name)){
                    Toast.makeText(getApplicationContext(), "Name must not empty. ", Toast.LENGTH_SHORT).show();
                }
            } else { 



                mDbHelper.insertScore(txt_name.getText().toString(), score);
                mDbHelper.close();

                Intent myIntent = new Intent(v.getContext(), ScoresActivity.class);
                startActivityForResult(myIntent, 0);

            }
         }
     });
}


private boolean isEmpty(EditText etText) {
    return etText.getText().toString().trim().length() == 0;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_arcade, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        mDbHelper.close();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

您已启动计时器但未停止计时器。计时器启动后,您需要取消倒计时。在onFinish()

中添加else部分
void startTimer() {
    countDownTimer = new CountDownTimer(currentTime, 1000) {
        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds) {
            seconds = leftTimeInMilliseconds / 1000;
            currentTime = leftTimeInMilliseconds;
            lbl_timer.setText(String.format("%02d", seconds / 60)
                    + ":" + String.format("%02d", seconds % 60));

        }

        @Override
        public void onFinish() {
            // this function will be called when the timecount is finished
                if(currentTime<1717){
                    Toast.makeText(getApplicationContext(), "TIMES UP! "+currentTime, Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(ArcadeActivity.this, ScoreActivity.class);
                    i.putExtra("myScore", score);
                    startActivity(i);
                    finish();
                } else{
                  //Added else block
                  this.cancel();

                }

        }

    }.start();

}

尚未测试过。但是你得到了问题所在。