如何每隔3秒发出一声哔哔声?

时间:2016-03-03 20:32:54

标签: android

我试图在我的代码上每隔3秒发出一声哔哔声。 我写了这段代码..但是有些东西不能正常工作 - 而且只有一次哔哔声出现而且我遇到了崩溃

我的应用程序运行时具有更多UI功能,因此我需要在不同的线程上发出蜂鸣声。

代码:

private Timer _timeTimeToBeep;
private TimerTask _task;
private Thread _thread;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    _displayValue = (TextView)findViewById(R.id.displayValue);

    displayValueUpdate();

    _timeTimeToBeep = new Timer();

    _task = new TimerTask() {

        @Override
        public void run() {


            _thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    playSound();
                }
            });

            _thread.start();
        }
    };

}


 public void playSound(){
    final ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
    toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);
}

 // START BEEP BUTTON HERE
 public void startPlayClickBtn(View view) throws InterruptedException {
    _timeTimeToBeep.schedule(_task, 3000);
}

2 个答案:

答案 0 :(得分:2)

更改你的startPlayClickBtn,如下所示:

// START BEEP BUTTON HERE
 public void startPlayClickBtn(View view) throws InterruptedException {
    _timeTimeToBeep.schedule(_task, 3000, 3000);
}

答案 1 :(得分:1)

实际上,您缺少 <div id="main"> <!-- center --> <div id="centerColumn"> <h1>&nbsp;</h1> <div class="fontSize" style="margin:50px;"> <h2>Oops, an unexpected error has occurred.</h2> <br/><br/><br/> Please <a class="colorMediumBlue bold hover fontSize" href="/">go home</a> and try again. <br/><br/><br/> If you receive this same message more than once, please email <a class="colorMediumBlue bold hover fontSize" href="mailto:support@flurry.com">support@flurry.com</a> with details on how you arrived on this error page. <br/><br/> We apologize for this inconvenience and we will find a solution to this issue as soon as possible. <br/><br/> Sincerely,<br/> The Flurry Team </div> </div> </div> 方法的一个参数。您必须将schedule传递给它。

像这样改变:

task, delay, period