倒数计时器在后台运行,尽管按下了后退键

问题描述 投票:1回答:2

我有一个在游戏活动中使用倒数计时器的应用程序。当按下后退按钮时,主要活动加载,但计时器不会停止。当时间在背景上结束时,回合活动结束将加载;即使主活动运行。所以我想提供计时器,当按下后退按钮时停止。我该怎么办?

    package com.example.tabuilengilizcenigelitir;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class GameActivity extends AppCompatActivity {

    public final static String TEAM_ONE_SCORE = "TEAM_ONE_SCORE";
    public final static String TEAM_TWO_SCORE = "TEAM_TWO_SCORE";
    public final static String TURN = "TURN";

    static private int team1Score;
    static private int team2Score;
    private int passNum,passNumCopy ;
    private int raundNum;
    private int raundTime;
    private int currentRound;
    private int size;
    private int language;
    private int timeLeft;

    private boolean turn ;
    boolean startTurn;

    CountDownTimer countDownTimer;
    ProgressBar mProgressBar;
    TextView time,teamName,guessWord,translateWord,tabuWord1,tabuWord2,tabuWord3,scoreText;
    ArrayList <Words> words;
    Button passButton,tabuButton,trueButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gamenew3);


        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mProgressBar.getProgressDrawable().setColorFilter(
                Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

        words= new ArrayList<Words>();

        teamName = (TextView) findViewById(R.id.teamName);
        scoreText=(TextView) findViewById(R.id.score);

        startTurn = true;
        turn = true ;
        teamNameText(turn);


        SharedPreferences sharedPref = this.getSharedPreferences("MyData",MODE_PRIVATE);
        raundNum = sharedPref.getInt("roundNum",10);
        raundTime = sharedPref.getInt("roundTime",60);
        passNum = sharedPref.getInt("pasHakki",3);
        language = sharedPref.getInt("language",1);
        passNumCopy = passNum;
        currentRound =1;

        timeLeft=raundTime;

        team1Score=0;
        team2Score=0;

        updateRoundNum();
        updateScores();

        time=(TextView) findViewById(R.id.timeText);

        startRoundTimer(false,raundTime);

        try {
            getCards(language);
        } catch (IOException e) {
            e.printStackTrace();
        }
        updateCards();
        updateScores();

        passButton = (Button) findViewById(R.id.passButton);
        tabuButton = (Button) findViewById(R.id.tabuButton);
        trueButton = (Button) findViewById(R.id.trueButton);

        assert passButton != null;
        assert trueButton != null;
        assert tabuButton != null;

        passButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(passNum>0){
                    updateCards();
                }
                passNum--;
                updatePas();
            }
        });

        tabuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(turn){
                    team1Score--;
                }
                else{
                    team2Score--;
                }
                updateCards();
                updateScores();
            }
        });

        trueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(turn)
                    team1Score++;
                else
                    team2Score++;
                updateCards();
                updateScores();
            }
        });

    }

    public int getTeam1Score(){
        int _team1Score = this.team1Score;
        return _team1Score;
    }
    public int getTeam2Score(){
        int _team2Score = this.team2Score;
        return _team2Score;
    }

    public void updateRoundNum(){
        TextView roundText = (TextView) findViewById(R.id.roundText);
        roundText.setText("Round:" + currentRound + "/" + raundNum);
        currentRound++;
    }

    public void updateScores(){
            if(turn){
                scoreText.setText(""+team1Score);
            }
            else{
                scoreText.setText(""+team2Score);
            }
    }

    public void teamNameText(boolean t){
        if(t) {
            teamName.setText("Team 1");
        } else
            teamName.setText("Team 2");
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_CANCELED) {
                updateScores();
                updateCards();
            }
        }
        if (requestCode == 2) {
            if (resultCode == Activity.RESULT_CANCELED) {
                startTurn = true;
                startRoundTimer(true,raundTime);
                updateScores();
                updateCards();
            }
        }
    }

    public void startRoundTimer(boolean started,int _raundTime){

        int mills;
        if (!started)
            mills = _raundTime*1000+1000;
        else
            mills = _raundTime*1000+1000;

        mProgressBar.setMax(_raundTime);

        countDownTimer= new CountDownTimer(mills,1000) {
            @Override
            public void onTick(long l) {
                time.setText(String.valueOf(l/1000));
                int progress = (int) (l/1000);
                mProgressBar.setProgress(progress);
                timeLeft=(int) l/1000;
            }

            @Override
            public void onFinish() {
                if(!turn) {
                    if(raundNum+1 == currentRound) {
                        Intent intent = new Intent(GameActivity.this, EndOfGameActivity.class);
                        startActivity(intent);
                    }
                    updateRoundNum();
                }
                countDownTimer.cancel();
                passNum = passNumCopy;
                turn =!turn;
                startTurn= false;
                timeLeft = raundTime;
                passButton.setAlpha((float)1.0);

                Intent i = new Intent(GameActivity.this, EndOfRoundActivity.class);
                i.putExtra(TEAM_ONE_SCORE, team1Score);
                i.putExtra(TEAM_TWO_SCORE, team2Score);
                i.putExtra(TURN, turn);
                startActivityForResult(i,2);

                if (startTurn) {
                    startRoundTimer(true,raundTime);
                }
                teamNameText(turn);

            }
        }.start();
    }
    public void getCards(int choice) throws IOException {
        InputStream input;
        BufferedReader file;

        switch (choice){
            case 1:{
                input = this.getResources().openRawResource(R.raw.cards_tr);
                file = new BufferedReader(new InputStreamReader(input));
                break;
            }
            case 2:{
                input = this.getResources().openRawResource(R.raw.cards_es);
                file = new BufferedReader(new InputStreamReader(input));
                break;
            }
            case 3:{
                input = this.getResources().openRawResource(R.raw.cards_ger);
                file = new BufferedReader(new InputStreamReader(input));
                break;
            }

            default:
                throw new IllegalStateException("Unexpected value: " + choice);
        }


        while(file.ready()){
            words.add(new Words(file.readLine(),file.readLine(),file.readLine(),file.readLine(),file.readLine()));
            file.readLine();
        }
        input.close();
        file.close();
    }

    public void updateCards(){
        guessWord= (TextView) findViewById(R.id.guessWord);
        translateWord =(TextView) findViewById(R.id.translateWord);
        tabuWord1= (TextView) findViewById(R.id.tabuWord1);
        tabuWord2= (TextView) findViewById(R.id.tabuWord2);
        tabuWord3= (TextView) findViewById(R.id.tabuWord3);

        size= words.size();
        int random = (int)(Math.random()*size);

        guessWord.setText(""+words.get(random).guessWord);
        translateWord.setText("("+words.get(random).translate+")");
        tabuWord1.setText(""+words.get(random).tabuWord1);
        tabuWord2.setText(""+words.get(random).tabuWord2);
        tabuWord3.setText(""+words.get(random).tabuWord3);
    }

    protected void onPause() {
        super.onPause();
        countDownTimer.cancel();
        //stop your timer here...
    }

    protected void onResume() {
        super.onResume();
        startRoundTimer(true,timeLeft);
    }

    public void onBackPressed(){
        super.onBackPressed();
        countDownTimer.cancel();
    }


    public void updatePas(){
        if(passNum==0){
            passButton.setAlpha((float) 0.5);
        }

    }
}

class Words {
    String translate,guessWord,tabuWord1,tabuWord2,tabuWord3;

    public Words (String w1,String w2,String w3,String w4,String w5){
        translate=w1;
        guessWord=w2;
        tabuWord1=w3;
        tabuWord2=w4;
        tabuWord3=w5;
    }

}
android android-studio back-button countdowntimer onbackpressed
2个回答
0
投票

使用此代码来管理您的计时器。在活动或片段的onPause方法中,发送false取消它。

public void startTimer(long time, boolean start) {

    if (start) {

        timer = new CountDownTimer(time, 1000) {

            public void onTick(long millisUntilFinished) {


            }

            public void onFinish() {


            }
        }.start();

    } else if (timer != null) {
        timer.cancel();
    }

}

0
投票

因为在onResume()方法中再次激活了计时器。实际上在这里:

protected void onResume() {
    super.onResume();
    startRoundTimer(true,timeLeft);
}

如果您在此处不再启动计时器,它将不会在后台运行。并再次检查这部分代码:

if (!started)
        mills = _raundTime*1000+1000;
    else
        mills = _raundTime*1000+1000;

这两个条件都具有相同的结果。您可以删除这些条件。

© www.soinside.com 2019 - 2024. All rights reserved.