如何在使用警报对话框时保持进程正常工作?

如何解决如何在使用警报对话框时保持进程正常工作?

我有一个方法,当我单击开始按钮两次时调用该方法,此操作调用该方法,然后弹出警告消息或对话框,询问用户他/她是要退出还是继续。退出部分正在工作,但是当我说 No 时,正在使用的倒数计时器进程停止了。如何弹出警报消息,但即使出现警报消息或单击“否”也能保持倒数计时器正常工作?

    private void AlertMessage()
{
    AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
            .setIcon(android.R.drawable.ic_dialog_alert)
//set title
            .setTitle("YOU ARE ABOUT TO EXIT!!!")
//set message
            .setMessage("Exiting will cancel this process")
//set positive button
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface,int i) {
                    //set what would happen when positive button is clicked
                    //RESETTING FIRST THEN RETURN TO MAIN MENU
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    if (Build.VERSION.SDK_INT >= 11)
                    {
                        recreate();
                        pb.setProgress(0);
                        pb2.setProgress(0);
                        pb3.setProgress(0);
                        Toast.makeText(tool1mode1.this,"Reset",Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Intent intent = getIntent();
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        finish();
                        overridePendingTransition(0,0);

                        startActivity(intent);
                        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                    }
                    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    Intent goingback = new Intent(tool1mode1.this,Settings.class);
                    tool1mode1.this.finish();
                    startActivity(goingback);
                    overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
                }
            })
//set negative button
            .setNegativeButton("No",int i) {
                    //set what should happen when negative button is clicked
                    Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
                    
                }
            })
            .show();
}

这是我通过按钮使用它时:

newbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clicked = clicked + 1;
            startTimer();
            //Check flow rate
            if(num == 450)
            {
                try {
                    flow.setText(String.valueOf(num));
                    progressBar.setIndeterminateDrawable(Circle);
                    progressBar.setVisibility(View.VISIBLE);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
            else if(num == 150)
            {
                try {
                    flow.setText(String.valueOf(num));
                    progressBar.setIndeterminateDrawable(Circle);
                    progressBar.setVisibility(View.VISIBLE);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }

            }

            if(clicked > 1)
            {
                AlertMessage();

            }

        }
    });

定时器方法:

    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();
                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 that the first if statement of key 60 is entered");
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;
                        progress2 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"TIME OF STAGE = "+time_of_stage);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();

                        setflowrate();

                    } else if ((millisPassed > 60000 && millisPassed <= 180000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);

                        Log.println(Log.ASSERT,"Check that the second if statement of key 60 is entered");
                        statusIfDown();

                        time_of_stage = (millisInFuture - (millisPassed+60000)) % 120000;    //CREDIT GOES TO BASHMOHNDES AMIN
                        progress3 = (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();

                    } else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,"Check that the first if statement of key 60 is entered");
                        statusIfDown();

                        time_of_stage = (millisInFuture - millisPassed) % 60000;
                        progress3 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();
                    }

                }
                //key:90 sec ----> 01:30
                else if (millisInFuture == 720000) {
                    if ((millisPassed <= 90000) || (millisPassed > 270000 && millisPassed <= 360000)
                            || (millisPassed > 450000 && millisPassed <= 540000)
                            || (millisPassed > 630000 && millisPassed <= 720000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepupimage.setAnimation(animation);
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 90000;
                        progress2 = (int) (time_of_stage*100 / 90000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();
                        setflowrate();
                    } else if ((millisPassed > 90000 && millisPassed <= 270000)) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture+90000) - millisPassed) % 180000;
                        progress3= (int) (time_of_stage*100 / 180000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();

                    } else if ((millisPassed > 360000 && millisPassed <= 450000) || (millisPassed > 540000 && millisPassed <= 630000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 90000;
                        progress3= (int) (time_of_stage*100 / 90000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }
                }
                //key:120 sec ----> 02:00
                else if (millisInFuture == 960000) {
                    if (millisPassed <= 120000 || millisPassed > 360000 && millisPassed <= 480000 || millisPassed > 600000 && millisPassed <= 720000 || millisPassed > 840000 && millisPassed <= 960000) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 120000;
                        progress2 = (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();
                        setflowrate();

                    } else if (millisPassed > 120000 && millisPassed <= 360000) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture+120000) - millisPassed) % 240000;
                        progress3= (int) (time_of_stage*100 / 240000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }  else if (millisPassed > 480000 && millisPassed <= 600000 || millisPassed > 720000 && millisPassed <= 840000) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 120000;
                        progress3= (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }
                }
                //key:20 sec ----> 00:20
                else if (millisInFuture == 160000) {
                    if (millisPassed <= 20000 || millisPassed > 60000 && millisPassed <= 80000 || millisPassed > 100000 && millisPassed <= 120000 || millisPassed > 140000 && millisPassed <= 160000) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 20000;
                        progress2 = (int) (time_of_stage*100 / 20000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();

                        setflowrate();
                    } else if (millisPassed > 20000 && millisPassed <= 60000) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture+20000) - millisPassed) % 40000;
                        progress3 = (int) (time_of_stage*100 / 40000);
                        Log.println(Log.VERBOSE,"progress2= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();
                    } else if (millisPassed > 80000 && millisPassed <= 100000 || millisPassed > 120000 && millisPassed <= 140000) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 20000;
                        progress3 = (int) (time_of_stage*100 / 20000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();
                    }
                }
                //key:30 sec ----> 00:30
                else if (millisInFuture == 240000) {
                    if (millisPassed <= 30000 || millisPassed > 90000 && millisPassed <= 120000 || millisPassed > 150000 && millisPassed <= 180000 || millisPassed > 210000 && millisPassed <= 240000) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 30000;
                        progress2 = (int) (time_of_stage*100 / 30000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();
                        setflowrate();
                    } else if (millisPassed > 30000 && millisPassed <= 90000) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture + 30000) - millisPassed) % 60000;
                        progress3 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    } else if (millisPassed > 120000 && millisPassed <= 150000 || millisPassed > 180000 && millisPassed <= 210000) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 30000;
                        progress3 = (int) (time_of_stage*100 / 30000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }
                }
                //key:60 sec ----> 01:00
                else if (millisInFuture == 480000) {
                    if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;
                        progress2 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
                        pb2.setProgress(progress2);
                        upArrowAnimation();
                        setflowrate();

                    } else if ((millisPassed > 60000 && millisPassed <= 180000)) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - (millisPassed+60000)) % 120000;    //CREDIT GOES TO BASHMOHNDES AMIN
                        progress3 = (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();

                    } else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;    //CREDIT GOES TO BASHMOHNDES AMIN
                        progress3 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setflowrate();

                    }

                }

            }

            @Override
            public void onFinish() {
                Toast.makeText(tool1mode1.this,"Done",Toast.LENGTH_SHORT).show();
                avd2.stop(); avd3.stop();
                try {
                    pb.setProgress(100); pb2.setProgress(0); pb3.setProgress(0);
                    stage_timer.setVisibility(View.INVISIBLE);
                    stage_timer.setVisibility(View.INVISIBLE);
                    progressBar.setVisibility(View.INVISIBLE);
                    progressBar2.setVisibility(View.INVISIBLE);
                    progressBar.setProgress(0);
                    progressBar2.setProgress(0);
                    progressBar.setTranslationY(60);
                    progressBar2.setTranslationY(60);
                    flow.setTranslationY(60);
                    animation1.cancel(); animation2.cancel();

                    Intent stoppump = new Intent(tool1mode1.this,stop_pump.class);
                    startActivity(stoppump);
                    //Vibration
                    if (Build.VERSION.SDK_INT >= 26) {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150,VibrationEffect.DEFAULT_AMPLITUDE));
                    } else {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150},new int[]{VibrationEffect.EFFECT_CLICK},-1));
                    }
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }

编辑:

我有另一个计时器,我用它在大计数器内的某些时间间隔内倒计时,另一个计时器似乎在我弹出警报对话框以及我拥有的任何其他动画时停止工作,只有大计数器继续工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-