如何从androidstudio中的倒计时计时器返回标志数组的值?

hof1towb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(408)

我有一个倒计时计时器,里面是ontick方法,在这个方法里面有几个if语句。现在,如果我已经在if语句中设置了标志数组的值,并且希望返回标志的值并在单击按钮时检查它们,那么我该如何做呢?

the array ---> int [] flag = {0,0,0}
the timer --->  

private void startTimer() {
        Log.println(Log.ASSERT, "CHECK","Entered startTimer() method");
        millisInFuture = mTimeLeftInMillis;
        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
                final long millisPassed = millisInFuture - mTimeLeftInMillis;
                progress = (int) (millisPassed * 100 / millisInFuture);
                pb.setProgress(progress);
                pb2.setProgress(0);
                pb3.setProgress(0);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //Key: 60 sec
                if (millisInFuture == 480000) {
                    if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//                        stepupimage.setAnimation(animation);
                        Log.println(Log.ASSERT,"CHECK","Check that the first if statement of key 60 is entered");
                        statusIfUp();
                        flag[0] = 1;
                        //stageUp(stageTime[0]);
                        upArrowAnimation();
                    } else if ((millisPassed > 60000 && millisPassed <= 180000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);
                        Log.println(Log.ASSERT,"CHECK","Check that the second if statement of key 60 is entered");
                        statusIfDown();
                        flag[0] = 0;
                        flag[1] = 1;
                        flag[2] = 0;
                       // stageDown(stageTime[1]);
                        downArrowAnimation();
                    } else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);
                        Log.println(Log.ASSERT,"CHECK","Check that the first if statement of key 60 is entered");
                        statusIfDown();
                        flag[0] = 0;
                        flag[1] = 0;
                        flag[2] = 1;
                      //  stageDown(stageTime[2]);
                        downArrowAnimation();
                    }

                }

            @Override
        public void onFinish() {

            pb.setProgress(100); pb2.setProgress(0); pb3.setProgress(0);
            stage_timer.setVisibility(View.INVISIBLE);
            stage_timer.setVisibility(View.INVISIBLE);
            avd1.stop(); avd2.stop(); avd3.stop(); avd4.stop();
            //Vibration
            if (Build.VERSION.SDK_INT >= 26) {
                ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
                Toast.makeText(tool1mode1.this, "Done", Toast.LENGTH_SHORT).show();
            } else {
                ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150}, new int[]{VibrationEffect.EFFECT_CLICK}, -1));
            }
        }
    }.start();

}

现在,当我单击onclick方法中的按钮时,我调用starttimer()方法来启动计时器。我想要的是在这个计时器工作时检查标志值,并且基于标志值我将调用一个方法,我将把一个值传递给它。这些旗子作为缺少更好词语的信号。那么如何在计时器内的ontick方法中返回标志值呢?

14ifxucb

14ifxucb1#

你需要回拨电话。一个简单的方法就是创建一个 Interface :

public interface Execute {

    public void doExecute(int[] flags);

}

然后打电话给你的朋友 StartTimer 新方法 Execute 像这样:

private void testMethod() {
    startTimer(new Execute(){

        @Override
        public void doExecute( int[] flags ) {
            // do something with your flags here
        }
    });
}

所以加上 Execute 对象作为 StarTimer 方法:

private void startTimer( Execute execute ) {
        Log.println(Log.ASSERT, "CHECK","Entered startTimer() method");
        //etc..

最后运行 doExecute 方法来自 startTimer 当你想的时候。例如:

} else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
//  Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//  stepdownimage.setAnimation(animation);
    Log.println(Log.ASSERT,"CHECK","Check that the first if statement of key 60 is entered");
    statusIfDown();
    flag[0] = 0;
    flag[1] = 0;
    flag[2] = 1;
    //  stageDown(stageTime[2]);
    downArrowAnimation();
    execute.doExecute(flag); // <-- run  
}

相关问题