我创建了一个计时器,它运行良好。现在我想使用计时器,让我们说,每5秒钟,我将获得用户的位置。然后我测试方法是否有效。所以我将oxy++
(MainActivity中的字段)放入其中,并查看它是否真的在每个5secs中运行。然后我遇到了问题。
public class MapsActivity extends BaseActivity /*implements LocationListener*/{
int oxy = 0;
private int isReset = 1;
private TextView textTimer;
private Button startButton;
private Button pauseButton;
private Button resetButton;
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private GoogleMap mMap;
private Button testbtn;
double x;
double y;
double j;
double k;
float dis;
TextView txt;
Location locationA;
Location locationB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
textTimer = (TextView) findViewById(R.id.textTimer);
startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
//LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Location locationA = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//x = locationA.getLatitude();
//y = locationA.getLongitude();
}
});
pauseButton = (Button) findViewById(R.id.btnPause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
}
});
resetButton = (Button) findViewById(R.id.btnReset);
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
textTimer.setText("0:00:00");
timeSwap=0;
}
});
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
}
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
int test = (int) (finalTime%5000);
if (test==0) {
oxy++;
}
txt.setText("o = "+ test+ " "+ oxy);
}
};
Runnable updateTimerMethod是一个循环,它每毫秒运行一次。 因此,如果我使用秒%5000,它会在每5秒运行,但是氧气保持++本身直到毫秒加起来将秒数增加到6。 然后我使用以毫秒为单位的finalTime到%5000。当我重新运行程序时,它不是每5秒工作,氧气+ 1不规则地以5的倍数运行,第一次说25秒,55秒,85秒氧气+ 1。然后我重新运行它,在45s,65s,105s中氧气+ 1。 有人能说出它为什么会发生吗?有什么方法可以解决这个问题吗?因为我想用它来获得每50秒一次的位置。在此先感谢!
答案 0 :(得分:0)
使用方法Handler.postDelayed
对于时间感觉来说不是一个好主意,因为它不够准确。而是使用TimeAnimator(API11 +,或下面的NineOldAndroids)。
TimeAnimator的基本用法:
TimeAnimator anim = new TimeAnimator();
anim.setTimeListener(new TimeAnimator.TimeListener() {
long time = 0;
@Override
public void onTimeUpdate(TimeAnimator timeAnimator, long t, long dt) {
time += dt;
if (time >= 5000) { // >= needed because this also might be not totally accurate...
time -= 5000; // keep the remainder (if there is) to correct the accuracy of next loop
// do stuff here (in every 5 seconds)
}
}
});