1

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

我该如何解决这个问题?

这是该类的代码:

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();

        }

}

天呐!

4

2 回答 2

3
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();
于 2012-08-31T07:09:51.250 回答
0

您需要查看 android 文档中的应用程序生命周期 - 如果您建议我认为您的应用程序只是被 android 关闭并重新启动 - 因此您的 onCreate 会再次被调用并重置计时器。

Android 应用程序不是简单的对象,您必须处理的部分问题是 opsys 在需要时将您推来推去。

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

于 2012-07-12T16:00:08.703 回答