Android Handler.postDelayed 只工作一次

Android Handler.postDelayed only working once

我正在编写一个试图通过振动来报时的 WearOS 应用程序。 (例如,如果时间是 4:12 它将振动 4 次,然后 1 次,然后 2 次) 我怀疑这有多少实际用途,但我这样做是为了了解 android 应用程序开发。

我正在尝试使用 handler.postDelayed 作为振动之间的延迟,但这只能使用一次。在它成功触发一次处理程序后,我需要重新启动 activity 才能使其再次工作。为什么?

我的相关代码在这里:

    int[] pulses = new int[4]; //length 4, one for each possible digit of the time

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //some init code goes here
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR);
        int minute = cal.get(Calendar.MINUTE);

        pulses[0] = hour/10;
        pulses[1] = hour%10;
        pulses[2] = minute/10;
        pulses[3] = minute%10;

        RelativeLayout parent_layout = findViewById(R.id.parent_layout);
        parent_layout.setOnTouchListener(new TotalGestureDetector(this){
            @Override
            public void onDoubleTap(MotionEvent e) {
                Toast.makeText(getApplicationContext(), "Double-Tap", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int time = vibratePattern();
                        if (time < 0){
                            return; //no need to re-call the function
                        }
                        new Handler().postDelayed(this, time);
                        return;
                    }
                }, 500);

            }
        });

    private int vibratePattern(){
        int pulse_len = 130;
        int pause_len = 100;
        int delay_between_digits = 1250;
        for(int i = 0; i < pulses.length; i++){
            if (pulses[i] > 0){
                pulses[i]--;
                buzz(pulse_len);
                return pulse_len + pause_len;
            } else if( pulses[i] == 0){
                //don't vibrate, insert delay between digits
                pulses[i]--; //set this to -1 so we know it is acted upon
                return delay_between_digits;
            } else {
                //Do nothing, let the loop continue to the next digit in the time.
            }
        }
        //if it ever gets here, array is empty
        //return -1 to show that no more calls are needed
        return -1;
    }

对不起,如果代码太多。

buzz 只是我的一个功能,它会根据给定的毫秒数振动设备

TotalGestureDetector 是一个 class,我刚刚从这里链接:https://gist.github.com/nesquena/b2f023bb04190b2653c7 并重命名。

parent_layout 实际上只是空白中的默认布局 activity -- 屏幕是空白的我希望这个应用程序只有振动。

pulses 已经全部振动时,您应该再次恢复 pulses 的值。试试这个

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        int time = vibratePattern();
        if (time < 0){
            pulses[0] = hour/10;
            pulses[1] = hour%10;
            pulses[2] = minute/10;
            pulses[3] = minute%10;
            return; //no need to re-call the function
        }
        new Handler().postDelayed(this, time);
        return;
    }
}, 500);