【发布时间】:2014-03-25 15:05:22
【问题描述】:
我正在构建一个应用程序,它会定期将 GPS 坐标发送到服务器并将它们存储在数据库中。但是当在数据库中看到时,我发现 gps 实际上后来发送了错误的(旧)坐标。
旧的意思是,假设我正在开车旅行,
At 2:00PM the coordinates are 73.982749854638 and 99.65363535375 (suppose)
At 2:05PM the coordinates are 73.982749812343 and 99.12334567866
现在我关闭了应用程序,然后再次运行它
6:00PM the coordiantes are 73.1234546789 and 99.123456789
但是当我在下午 6:00 签入服务器数据库时,服务器实际上收到了下午 2:05 的坐标
我不明白为什么会这样,下面是我的代码,
public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
protected SendLocation sendLoc;
protected DeviceInformation devInfo;
TextView txtLat, status;
Button regDevice;
ToggleButton toggBtn;
String lat, provider;
protected long minTime = 60000;
protected Double latt, lonn;
protected boolean toggle;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
devInfo = new DeviceInformation(MainActivity.this);
txtLat = (TextView) findViewById(R.id.textview1);
status = (TextView) findViewById(R.id.status);
toggBtn = (ToggleButton) findViewById(R.id.toggleButton1);
toggBtn.setText("Start Tracking!");
toggBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(toggBtn.isChecked()){
status.setText("Tracking is Enabled!");
toggBtn.setText("Stop Tracking!");
status.setTextColor(Color.BLUE);
toggle = true;
}else{
status.setText("Tracking is Disabled!");
toggBtn.setText("Start Tracking!");
status.setTextColor(Color.RED);
toggle = false;
}
}
});
final Context context = this;
regDevice = (Button) findViewById(R.id.regButton);
regDevice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Register.class);
startActivity(intent);
}
});
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, 0, this);
}
@Override
public void onLocationChanged(Location location) {
latt = location.getLatitude();
lonn = location.getLongitude();
txtLat = (TextView) findViewById(R.id.textview1);
if(toggle == true){
txtLat.setText(latt+"\n"+lonn);
String uniq = devInfo.imei;
String aobj[] = new String[3];
aobj[0]=Double.toString(latt);
aobj[1]=Double.toString(lonn);
aobj[2]=uniq;
sendLoc = new SendLocation();
sendLoc.execute(aobj);
}
else if(toggle == false){
//Toast.makeText(getApplicationContext(), "Not Sending", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
toggBtn.setEnabled(false);
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("GPS Disabled! Please Enable GPS.");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
toggBtn.setEnabled(true);
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Click button below to start tracking.");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}
【问题讨论】:
标签: android android-intent gps