我需要你的帮助。我有一项活动,每 5 分钟获取一次当前的 lat lon gps = new GpsData(StartActivity.this);
。这很好用!但是当我离开一个县城时,活动又刷新了。然后我没有得到当前的 lon lat。我不知道为什么。
这是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
btnShowLocation = (Button) findViewById(R.id.report_btn);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(StartActivity.this, ReportActivity.class);
startActivity(intent);
finish();
}
});
}
@Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
//start GPS
gps = new GpsData(StartActivity.this);
//start json download
new task().execute();
}
});
}
}, 0, 300000); // updates each 5 min
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(StartActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Status Update...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
//get Location Data
double latitude = gps.latitude;
double longitude = gps.longitude;
String mlat = String.valueOf(latitude);
String mlon = String.valueOf(longitude);
//if (gps.latitude != 0.0) {
JSONObject json = jsonFunctions.getJSONfromURL("http://myurl);
这是我的 GpsData
public class GpsData extends Service implements LocationListener {
public GpsData(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
// Get the location manager
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
return null;
}
/* Request updates at startup */
protected void onStart() {
}
/* Remove the locationlistener updates when Activity is paused */
protected void onPause() {
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
time = location.getTime();
speed = location.getSpeed();
}
这很简单:我希望每 5 分钟获取一次当前纬度,并使用当前县的当前数据刷新列表视图……我该怎么做?