简体   繁体   English

时间选择器每分钟执行安排的任务,直到达到设置的时间,而不是仅在设置的时间执行

[英]Time picker performs scheduled task every minute until set time is reached instead of performing it only at set time

I am using a Time Picker on which the user will set a time and then a toast message will be displayed at that time. 我正在使用Time Picker ,用户将在该Time Picker上设置时间,然后在该时间显示敬酒消息。 The problem is that the toast is being displayed every minute until it reaches the set time. 问题是烤面包每分钟显示一次,直到达到设置的时间。

Here are the codes: 以下是代码:

public class ManualControlsFragment extends Fragment {

 private TimePicker tp;
    private Calendar calendar;
    long pickedTimeInMillis;
    long timeDifference;

//some Fragment methods

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       View view = inflater.inflate(R.layout.fragment_manual_controls, container, false);

       tp = (TimePicker) view.findViewById(R.id.lightTimePickerStart);
       tp.setIs24HourView(true);
       calendar = Calendar.getInstance();

       tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                pickedTimeInMillis= calendar.getTimeInMillis();
                // at this point get the current time
                Calendar cal = Calendar.getInstance();
                // now calculate the difference as the user actually selected a time
                timeDifference = pickedTimeInMillis - cal.getTimeInMillis();

                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //Do something here
                        Toast.makeText(getActivity().getApplicationContext(), "Bulb is now on", Toast.LENGTH_LONG).show();
                    }
                }, timeDifference);
            }
        });

return view;
    }

I think it's because the onTimeChangedListener in time picker registers each minute while I scroll down the picker to select a minute. 我认为这是因为时间选择器中的onTimeChangedListener每分钟都会注册一次,而我向下滚动选择器以选择一分钟。 Like suppose I have to set the time to 14:30 and currently it is 14:25 on the time picker, I have to scroll down the picker from 25, 26, 27, 28, 29 and then finally to 30 to set it to 14:30. 就像假设我必须将时间设置为14:30,当前在时间选择器上将其设置为14:25一样,我必须从25、26、27、28、29向下滚动选择器,然后最后到30来将其设置为14:30。 I think this is why the time picker displays the toast at every minute. 我认为这就是为什么时间选择器每分钟显示吐司的原因。 How can I fix this? 我怎样才能解决这个问题? And also, I am using Looper.getMainLooper()) in the Handler because I have 4 other threads running in the background in some other fragment. 另外,我在Handler中使用Looper.getMainLooper()) ,因为在其他片段中,我还有4个其他线程在后台运行。

Try something like this, mHandler.removeMessages(0); 试试这样的东西, mHandler.removeMessages(0); will stop the hundler if it's already running, and the last one won't be stoped : 如果hundler已经在运行,它将停止它,并且最后一个不会停止:

 public class ManualControlsFragment extends Fragment {
    private TimePicker tp;
    private Calendar calendar;
    long pickedTimeInMillis;
    long timeDifference;
//some Fragment methods
    Handler mHandler = new Handler(Looper.getMainLooper());

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_manual_controls, container, false);
        tp = (TimePicker) view.findViewById(R.id.lightTimePickerStart);
        tp.setIs24HourView(true);
        calendar = Calendar.getInstance();
        tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                pickedTimeInMillis = calendar.getTimeInMillis();
                // at this point get the current time
                Calendar cal = Calendar.getInstance();
                // now calculate the difference as the user actually selected a time
                timeDifference = pickedTimeInMillis - cal.getTimeInMillis();
                mHandler.removeMessages(0);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //Do something here
                        Toast.makeText(getActivity().getApplicationContext(), "Bulb is now on", Toast.LENGTH_LONG).show();
                    }
                }, timeDifference);
            }
        });
        return view;
    }
}

Per the TimePicker documentation, the listener does the following: 根据TimePicker文档,侦听器执行以下操作:

Set the callback that indicates the time has been adjusted by the user. 设置指示时间已由用户调整的回调。

So I think your conclusion is correct by saying when the user scrolls through the timepicker it is repeatedly calling the callback defined in the listener. 因此,我认为您的结论是正确的,可以说用户在时间选择器中滚动时,它正在反复调用侦听器中定义的回调。

Try getting the user defined time from TimePicker.getHour() and TimePicker.getMinute() and compare it to the current system time's minute and hour value every 30 seconds in a new thread. 尝试从TimePicker.getHour()TimePicker.getMinute()获取用户定义的时间,并在新线程中每30秒将其与当前系统时间的分钟和小时值进行比较。 That should accomplish what you need. 那应该完成您需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM