我是Android开发的新手,但我正在努力让自己成为一名高尔夫测距仪。我有这个活动 -
public class hole_1 extends Activity implements View.OnClickListener {
Button nextBtn;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hole_1);
gps = new GPSTracker(hole_1.this);
// Get Variable From Home Activity
Bundle extras = getIntent().getExtras();
String course = null;
if (extras != null) {
course = extras.getString("Course");
}
//Set Next Hole Button
nextBtn = (Button) findViewById(R.id.nextButton);
nextBtn.setOnClickListener(this);
gps = new GPSTracker(hole_1.this);
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
double lat2 = 39.765718;
double lon2 = -121.860080;
Location loc1 = new Location("");
loc1.setLatitude(latitude);
loc1.setLongitude(longitude);
Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
int myDist = (int) (distanceInMeters * 1.0936);
TextView latView = (TextView) findViewById(R.id.yardage);
latView.setText(String.valueOf(myDist));
}else{
gps.showSettingsAlert();
}
}
@Override
public void onClick(View v) {
Intent myIntent = new Intent(this, end.class);
myIntent.putExtra("Hole",1);
startActivity(myIntent);
}
}
我想要做的是更新myDist变量,它是我当前坐标和固定坐标(lat2,lon2)之间的距离。我已经做了一些研究,发现asyncTask,线程和设置计时器,但无法找出这个应用程序的最佳方法..该应用程序工作得很好,但我必须刷新页面以获得更新的距离,并希望它只是更新每隔几秒钟......我该怎么办?
谢谢!
答案 0 :(得分:2)
1)您无法每隔几秒更新一次。 GPS每30秒更新一次。
2)你不会在这里使用异步任务或任何其他形式的线程,GPS可以在回调系统上运行。您请求更新,只要有更新,它就会给您回电。
3)不要使用GpsTracker LIBRARY EVER。它坏了。厉害。请参阅我在http://gabesechansoftware.com/location-tracking/
打破原因的完整说明