1

我的计时器有问题。然后我按下处理程序中的开始按钮值显示 -22406914:0-51 而不是倒计时。下面是代码(我通过这个帮助帮助了自己:http ://android-developers.blogspot.com/2007/11/stitch-in-time.html )。我不知道我在哪里做错了......

public class biegi extends Activity {
TextView Todleglosc, Tpredkosc, Tczas;
ImageView Ibiegacz;
Button clear, startstop;
LocationManager locationManager;
Location lokacjaPoczatkowa;
String providerName;
boolean start;
private long startTime;
private Handler mHandler = new Handler();

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.biegi);
inicjuj();
actionbuttonsc();
} // oncreate

private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
             final long start = startTime;
               long millis = SystemClock.uptimeMillis() - start;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;

               if (seconds < 10) {
                   Tczas.setText("" + minutes + ":0" + seconds);   
               } else {
                   Tczas.setText("" + minutes + ":" + seconds);            
               }     
               mHandler.postAtTime(this,start + (((minutes * 60) + seconds +     1) * 1000));               
        }           
    };

public void inicjuj (){
Todleglosc=(TextView) findViewById(R.id.odleglosc);
Tpredkosc=(TextView) findViewById(R.id.predkosc);
Tczas=(TextView) findViewById(R.id.czas);
Ibiegacz=(ImageView) findViewById(R.drawable.biegacz);
clear=(Button) findViewById(R.id.clear);
startstop=(Button) findViewById(R.id.button_bieg_start_stop);
start=false;
}


public void actionbuttonsc()
{
clear.setOnClickListener(new View.OnClickListener()
        {
    public void onClick(View v) {
    Tczas.setText("00:00:00");

    }
});

startstop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {   
        if (start==false)
        {
            Tczas.setText("00:00:01");
            startTime = System.currentTimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);
            start=true;
        }

else if (start==true)
    {
        Tczas.setText("00:00:00");
        mHandler.removeCallbacks(mUpdateTimeTask);
        start=false;                        
    }}});
}
}
4

1 回答 1

1

startTime使用currentTimeMillis( ) 设置,然后使用( )startTime = System.currentTimeMillis();读取它。uptimeMillislong millis = SystemClock.uptimeMillis() - start;

这是我要做的:

// Change this:
long millis = SystemClock.uptimeMillis() - start;
// .. to this:
long millis = System.currentTimeMillis() - start;
于 2012-08-08T20:53:47.810 回答