Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 屏幕锁定后定时器复位_Java_Android - Fatal编程技术网

Java 屏幕锁定后定时器复位

Java 屏幕锁定后定时器复位,java,android,Java,Android,我已经为扑克盲人创建了一个带有倒计时的小应用程序,但是我注意到当锁屏打开时,计时器会从一开始就重置 我如何解决这个问题 这是该类的代码: public class timer extends Activity { /** Called when the activity is first created. */ TextView contoallarovescia; TextView clicca; public boolean

我已经为扑克盲人创建了一个带有倒计时的小应用程序,但是我注意到当锁屏打开时,计时器会从一开始就重置

我如何解决这个问题

这是该类的代码:

public class timer extends Activity {
        /** Called when the activity is first created. */

        TextView contoallarovescia;
        TextView clicca;
        public boolean touchattivo=false;
        public int giro=1;
        public int girosecondi=8*60;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            this.requestWindowFeature(Window.FEATURE_NO_TITLE);

             super.onCreate(savedInstanceState);
             setContentView(R.layout.timer);

            contoallarovescia = (TextView) this.findViewById(R.id.contoallarovescia);
            Typeface typeFace=Typeface.createFromAsset(getAssets(),"font/LCDMB___.TTF");
            contoallarovescia.setTypeface(typeFace);

            clicca = (TextView) this.findViewById(R.id.clicca);
            chiamatimer(girosecondi*1000,1000);

        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // MotionEvent object holds X-Y values


            if (touchattivo)
            {

                chiamatimer(girosecondi*1000,1000);

            }

            return super.onTouchEvent(event);
        }


        public void chiamatimer(int secondi, int ciclo)
        {


            touchattivo=false;
            clicca.setVisibility(View.INVISIBLE);

            new CountDownTimer(secondi, ciclo) {




                 public void onTick(long millisUntilFinished) {

                     int sec =(int) (millisUntilFinished / 1000);


                     String result = String.format("%02d:%02d", sec / 60, sec % 60);


                     contoallarovescia.setText(result);
                 }

                 public void onFinish() {


                     String result = String.format("%02d:%02d", 0,  0);
                     giro++;

                     if (giro==5)
                     {

                         girosecondi=6*60;

                     }

                     contoallarovescia.setText(result);

                     clicca.setVisibility(View.VISIBLE);
                     touchattivo=true;

                     try {
                            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                            r.play();
                        } catch (Exception e) {}




                 }
              }.start();

        }

}

Tnx

您需要查看android文档中的应用程序生命周期-在您建议的情况下,我会想象您的应用程序只是被android关闭并重新启动-因此您的onCreate再次被调用,计时器被重置

Android应用程序不是简单的对象,你需要处理的一部分是opsys在需要时推着你


具体来看一下onPause,以及如何在稍后传递给onCreate的包中存储结束时间。这将解决您当前的错误。

但是计时器不会暂停,而是继续倒计时,当计数变为0时,给我allarm。也许这可以帮助我?
pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
        pmWL = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
        pmWL.acquire();

        mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        mKeyguardLock = mKeyguardManager.newKeyguardLock("activity_classname");
        mKeyguardLock.disableKeyguard();