Android处理程序重置

时间:2013-04-10 12:56:49

标签: android handler reset

我使用以下代码和处理程序在我的设备完全平坦(加速度计x,y值)5秒后产生哔声。如果设备仍然平坦,则声音每5秒再现一次。问题是代码工作正常第一次,我将设备平坦,但如果它不平,并尝试再将它放平,处理程序不起作用,声音立即生成,并遵循“lastBip”语句。我想每次设备不平坦时我必须尝试重置处理程序,但我不知道如何。我想要的是,当我的设备是平的时,声音应该在我的应用程序运行的所有时间后5秒内工作,而不仅仅是我第一次打开它时。有人知道我必须改变我的代码才能正常工作吗?提前谢谢..

 handler.postDelayed(new Runnable() {
   @Override
   public void run() {

    if (x > -0.1 && x < 0.1 && y > -0.1 && y < 0.1 && System.currentTimeMillis() - lastBip > bipRate) {

        AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_RING);
        float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_RING);
        float volume = streamVolumeCurrent / streamVolumeMax;
        lastBip = System.currentTimeMillis();
        soundPool.play(bipSoundID, volume, volume, 1, 0, 1);

    }

   }

 }, 5000);

0 个答案:

没有答案