我是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,线程和设置计时器,但无法找出此应用程序的最佳方法。.该应用程序按原样运行良好,但我必须刷新页面以获取更新的距离,并希望它进行更新本身每隔几秒钟。.我该怎么办?
谢谢!
1)您不能每隔几秒钟更新一次。GPS仅每30秒更新一次。
2)您不会在此处使用异步任务或任何其他形式的线程,GPS可以在回调系统上工作。您请求更新,只要有可用更新,它就会回叫您。
3)永远不要使用GpsTracker库。坏了 不好 在http://gabesechansoftware.com/location-tracking/上查看我有完整的文章,了解其为何会损坏
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句