Android studio从我的计时器中获取时间,以获取距离
我已经创建了一个计时器,它工作得很好。现在我想使用计时器,比方说,每5秒,我会得到用户的位置。然后我测试这个方法是否有效。所以我把Android studio从我的计时器中获取时间,以获取距离,android,Android,我已经创建了一个计时器,它工作得很好。现在我想使用计时器,比方说,每5秒,我会得到用户的位置。然后我测试这个方法是否有效。所以我把oxy++(MainActivity中的字段)放进去,看看它是否真的每5秒运行一次。然后我遇到了一个问题 public class MapsActivity extends BaseActivity /*implements LocationListener*/{ int oxy = 0; private int isReset = 1; private Text
oxy++
(MainActivity中的字段)放进去,看看它是否真的每5秒运行一次。然后我遇到了一个问题
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秒运行一次,但oxy会保持++本身,直到毫秒相加,将秒数增加到6。
然后我使用finalTime,以毫秒为单位,到%5000。它不是每5秒运行一次,当我重新运行程序时,oxy+1不规则地以5的倍数运行,比如说,oxy+1在25秒、55秒、85秒中第一次运行。然后我重新运行它,氧+1在45秒,65秒,105秒。
有人能告诉我为什么会这样吗?有什么办法解决这个问题吗?因为我想每50年代用这个来确定位置。提前谢谢 使用method
Handler.postDelayed
对于时间感的东西不是一个好主意,因为它不够准确。而是使用(API11+,或下面的NineodelDroids)
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)
}
}
});
你能告诉我更多关于如何使用TimeAnimator的信息吗?所以我应该在startbutton的代码中添加这个?如果是,我应该在哪里添加txt.setText(oxy),为了检查它是否真的每5秒运行一次?