如何克服每秒显示Toast的显示滞后

时间:2015-02-05 09:02:50

标签: android

My Toast与Timer秒不同步,如何克服显示滞后

CountDownTimer timer = new CountDownTimer(20000, 1000) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            Toast.makeText(getApplicationContext(), "Disabling Password, Please wait : " + millisUntilFinished/1000, Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onFinish() {
                                            Log.d("Admin", "Disabling Password");
                                            devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
                                            devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
                                            boolean resulttwo = devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
                                            Log.d("Admin", "Disabling Password : " + resulttwo);
                                            Toast.makeText(getApplicationContext(), "Congratulations!! Password Disabled", Toast.LENGTH_SHORT).show();
                                        }
                                }.start();

1 个答案:

答案 0 :(得分:2)

Toast提供了取消方法,

将mToast定义为数据成员并初始化为null

 CountDownTimer timer = new CountDownTimer(20000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(getApplicationContext(), "Disabling Password, Please wait : " + millisUntilFinished / 1000, Toast.LENGTH_SHORT);
            mToast.show();
            Log.d("Admin", "Disabling Password, Please wait : " + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            Log.d("Admin", "Disabling Password");
            devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
            devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
             boolean resulttwo = devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
            Log.d("Admin", "Disabling Password : ");
            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(getApplicationContext(), "Congratulations!! Password Disabled", Toast.LENGTH_SHORT);
            mToast.show();
        }
    }.start();