我想每15分钟访问一次用户位置,即纬度和经度,并将它们存储在CSV文件中。我怎样才能做到这一点?
我有一个GPSTracker
课程:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 15; // 15 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS() {
if (locationManager != null) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
和MainActivity
:
File root = Environment.getExternalStorageDirectory();
File gpxfile = new File(root, "mydata.csv");
try {
writer = new FileWriter(gpxfile);
writeCsvHeader("DateTime", "Latitude", "Longitude");
writeCsvData(""+strDate,latitude,longitude);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
private void writeCsvHeader(String h1, String h2, String h3) throws IOException {
String line = String.format("%s,%s,%s\n", h1,h2,h3);
writer.write(line);
}
private void writeCsvData(String date, double lat, double lon) throws IOException {
String line = String.format("%s,%f,%f\n", date, lat, lon);
writer.write(line);
}
对于我引用的CSV http://antipastohw.pbworks.com/w/page/41913616/Write%20CSV%20files%20onto%20SD%20Card%20Storage
答案 0 :(得分:1)
对于执行重复性任务,您可以使用Handler,
handler.postDelayed(new Runnable(){
@Override
public void run() {
// get Location or do whatever you want
}
}, (15 * 60 * 1000));
在这种情况下,getLocation()将在每15分钟后调用一次。
答案 1 :(得分:0)
可能这样可能
Timer timer = new Timer();
timerTask = new TimerTask() {
Handler mnhandler = new Handler();
@Override
public void run() {
mnhandler.post(new Runnable() {
@Override
public void run() {
//Use ur method which store CSV file
}
});
}
};
timer.schedule(timerTask, start, duration);