活动改变时,倒数计时器不会停止

时间:2016-04-05 10:54:03

标签: java android eclipse performance countdowntimer

我使用了具有计时器功能的类。所以我可以随时随地重用该代码。问题是当我从一个活动移动到另一个活动时,应该重置计时器但是计时器无法完成并且新的计时器将再次启动。这是我的代码

public class Utils {
    Context mContext;

    private static final String TAG = Utils.class.getSimpleName();
    CountDownTimer mCountDownTimer = null;
    boolean isRunning = false;
    private Activity activityContext;

    public Utils(Context context) {
        this.mContext = context;
        this.activityContext = (Activity) context;
    }

public void resetTimer() {

        if (isRunning) {
            Log.d(TAG, "Timer is stopping now..######");
            mCountDownTimer.cancel();
            mCountDownTimer = null;
            isRunning = false;
            startCountDownTimer();

        } else {
            Log.d(TAG, "Timer is starting now..######");
            startCountDownTimer();

        }

    }

    public void stopCountDownTimer() {

        if (mCountDownTimer != null) {

            mCountDownTimer.cancel();
            mCountDownTimer = null;

        }

    }

    public void startCountDownTimer() {

        mCountDownTimer = new CountDownTimer(10000, 1000) {

            public void onTick(long millisUntilFinished) {
                Log.d(TAG, "Timer is ticking..!!!!");

                isRunning = true;

            }

            public void onFinish() {
                Log.d(TAG, "Timer is finished..!!!!");
                isRunning = false;

                try {

                    Intent launchIntent = mContext.getPackageManager()
                            .getLaunchIntentForPackage("com.#########.###");
                    activityContext.startActivity(launchIntent);
                    activityContext.finish();
                    System.exit(0);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }.start();

    }
}

在活动创建方法中,我调用此resetTimer(),但计时器不会重置。我也打电话给stopCountDownTimer(),结果也一样。我在这里缺少什么?

已解决:

    public class Utils {
        Context mContext;

        private static final String TAG = Utils.class.getSimpleName();
        CountDownTimer mCountDownTimer = null;
        boolean isRunning = false;
        private Activity activityContext;

        private final long startTime = 10 * 1000;

        private final long interval = 1 * 1000;

        public Utils(Context context) {
            this.mContext = context;
            this.activityContext = (Activity) context;
            mCountDownTimer = new MyCountDownTimer(startTime, interval);
        }

    public void resetTimer() {

            if (isRunning) {
                Log.d(TAG, "Timer is stopping now..######");
                mCountDownTimer.cancel();
                mCountDownTimer = null;
                mCountDownTimer = new MyCountDownTimer(startTime, interval);
                isRunning = false;
                Log.d(TAG, "Timer is stopped amd starting again..######");
                mCountDownTimer.start();
                // startCountDownTimer();

            } else {
                Log.d(TAG, "Timer is starting now..######");
                // startCountDownTimer();
                mCountDownTimer.start();

            }

        }
    }
    public void stopCountDownTimer() {

        if (mCountDownTimer != null) {

            mCountDownTimer.cancel();
            mCountDownTimer = null;

        }

    }

    public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            Log.d(TAG, "Timer is finished..!!!!");
            isRunning = false;

            try {

                Intent launchIntent = mContext.getPackageManager()
                        .getLaunchIntentForPackage("com.####.####");
                activityContext.startActivity(launchIntent);
                activityContext.finish();
                System.exit(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {
            Log.d(TAG, "Timer is ticking..!!!!");

            //Toast.makeText(mContext, ""+millisUntilFinished/1000, 50).show();
            isRunning = true;

        }
    }

}

0 个答案:

没有答案