2015-07-18 80 views
1

我想要有一个定时器对话框,它将从0开始,每5秒发出一个声音。它适用于Nexus S API 22的仿真器,但不适用于我的HTC One M8 API 21手机。记录日志消息并在模拟器上听到声音,但在我的实际电话上都没有发生,所以我认为这不是一个合理的问题。为什么if语句在仿真器上工作,但不在实际设备上工作

private Runnable updateTimerThread = new Runnable() { 

    public void run() { 
     timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

     updatedTime = timeSwapBuff + timeInMilliseconds; 

     int secs = (int) (updatedTime/1000); 
     int mins = secs/60; 
     secs = secs % 60; 
     int milliseconds = (int) (updatedTime % 1000); 

     if(secs > 0 && ((secs%5) == 0) && milliseconds == 0){ 
      Log.d("BEEP", "beep"); 
      tg.startTone(ToneGenerator.TONE_PROP_BEEP); 
     } 

     timerValue.setText("" + mins + ":" 
       + String.format("%02d", secs) + ":" 
       + String.format("%03d", milliseconds)); 
     customHandler.postDelayed(this, 0); 
    } 
}; 

这里也是用于参考的onCreate和变量名。

public class TimerDialogFragment extends DialogFragment implements DialogInterface.OnClickListener{  

private TextView timerValue; 

private long startTime = 0L; 

private Handler customHandler = new Handler(); 

long timeInMilliseconds = 0L; 
long timeSwapBuff = 0L; 
long updatedTime = 0L; 

ToneGenerator tg; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    super.onCreateDialog(savedInstanceState); 
    View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_timer,null); 

    tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100); 

    timerValue = (TextView) v.findViewById(R.id.timerText); 
    startTime = SystemClock.uptimeMillis(); 
    customHandler.postDelayed(updateTimerThread, 0); 



    return new AlertDialog.Builder(getActivity()) 
      .setView(v) 
      .setMessage("") 
      .setCancelable(true) 
      .setNegativeButton("Continue with instructions",this) 
      .create(); 
} 

.... 

} 
+0

为什么postDelayed()为0的值?这根本不符合逻辑。 – BladeCoder

+0

我想实际更新时钟,以显示时间进度。 –

+0

您应该使用非零延迟来避免CPU烧毁。每100ms更新计时器显示应该足够了。 – BladeCoder

回答

1

在这个片断:

if(secs > 0 && ((secs%5) == 0) && milliseconds == 0){ 
     Log.d("BEEP", "beep"); 
     tg.startTone(ToneGenerator.TONE_PROP_BEEP); 
    } 

你在milliseconds计数是为了为0的蜂鸣声。这不是一个可靠的假设 - 这是一种非常糟糕的编码习惯。如果你想发出蜂鸣声,每5秒,改变你的Runnable这样:

private Runnable updateTimerThread = new Runnable() { 
    public void run() { 
    timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

    updatedTime = timeSwapBuff + timeInMilliseconds; 

    int secs = (int) (updatedTime/1000); 
    int mins = secs/60; 
    secs = secs % 60; 
    int milliseconds = (int) (updatedTime % 1000); 

    // if(secs > 0 && ((secs%5) == 0) && milliseconds == 0){ // <<<<<<<<< 
    Log.d("BEEP", "beep"); 
    tg.startTone(ToneGenerator.TONE_PROP_BEEP); 
    // } // <<<<<<<<< 

    timerValue.setText("" + mins + ":" 
      + String.format("%02d", secs) + ":" 
      + String.format("%03d", milliseconds)); 
    customHandler.postDelayed(this, 5000); // <<<<<<<<< 
    } 
}; 
+0

它是不是可靠的,因为它是一个敏感的时间框架?如果我按照你的方式来做,那么时钟每5秒钟只更新一次,这并不是我想要的。 –

+0

您可以有另一个不断更新时钟的处理程序。但它应该是另一个处理程序。而且它不可靠,因为你不能保证定时器总是达到5.000秒。它可能达到4.991,然后达到5.002。这样,你会错过第二个5 ... – Kasra

+0

是的,这就是我的意思是敏感的时间框架。为什么分开处理程序,而不是单独运行在同一个处理程序上做不同的事情? –

相关问题