2017-04-16 12 views
-1

2時間の間に時間を計算する必要があります。 System.currentTimeMillis()は、Threadを呼び出すたびに同じ値を返します。 私のコードは次のとおりです。ここでSystem.currentTimeMillis()は同じタイムスタンプを返します

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // Other codes.. 
    start_sec = Math.round(System.currentTimeMillis()/1000); 
    fin = false; 
    runThread(); 
    // Other codes.. 
} 

private void runThread() { 
    new Thread() { 
     public void run() { 
      while (i++ < 61) { 
       if (!running) return; 
       try { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          if(!fin){ 
           int len = Math.round(System.currentTimeMillis()/1000) - start_sec; 
           Log.d("current time: ",String.valueOf( Math.round(System.currentTimeMillis()/1000))); 
           Log.d("difference is: ", String.valueOf(len)); 
           if(len < 0 && len > 58){ 
            fin=true; 
           } 
           timerec.getLayoutParams().width = metrics.widthPixels *(60- len)/60; 
           timerec.requestLayout(); 
          } 
          else{ 
           end_game(); 
           running= true; 
          } 
         } 
        }); 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }.start(); 
} 

はログです:

... 
D/current time:: 1492337024 
D/difference is:: 0 
D/current time:: 1492337024 
D/difference is:: 0 
.... 

それは同じ "時間" をreturs。解決策は何ですか?

答えて

0

恐らくMath.round()が問題となります。

long len = System.currentTimeMillis()/1000 - start_sec; 
Log.d("current time: ",String.valueOf(System.currentTimeMillis()/1000)); 
Log.d("difference is: ", String.valueOf(len)); 

このコードは、アルファベットの分割を行います。

0

長い時間がかかります。それを1000で割ってはいけません。時差は秒数分の1です。なぜなら、それが丸めているのと同じ時間を示しているからです。

0

whileループの2つのサイクルの差は1秒未満です。差を秒単位で計算すると(現在のミリ秒を1000で割ったとき)、同じ秒があり、その差は0秒です。

違いをミリ秒で(分けずに)印刷してみます。

0

これを試してみてください:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // Other codes.. 
    start_sec = System.currentTimeMillis(); 
    fin = false; 
    runThread(); 
    // Other codes.. 
} 

private void runThread() { 
    new Thread() { 
     public void run() { 
      while (i++ < 61) { 
       if (!running) return; 
       try { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          if(!fin){ 
           int len = System.currentTimeMillis() - start_sec; 
           Log.d("current time: ",String.valueOf( System.currentTimeMillis())); 
           Log.d("difference is: ", String.valueOf(len)); 
           if(len < 0 && len > 58){ 
            fin=true; 
           } 
           timerec.getLayoutParams().width = metrics.widthPixels *(60- len)/60; 
           timerec.requestLayout(); 
          } 
          else{ 
           end_game(); 
           running= true; 
          } 
         } 
        }); 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }.start(); 
} 
関連する問題