有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个倒计时,里面是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方法中返回标志值呢


共 (1) 个答案

  1. # 1 楼答案

    您需要使用回调。一种简单的方法是创建一个Interface

    public interface Execute {
    
        public void doExecute(int[] flags);
    
    }
    

    然后用新的Execute调用StartTimer方法,如下所示:

    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..
    

    最后,在需要时从startTimer运行doExecute方法。例如:

    } 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  
    }