gpt4 book ai didi

android - 每 15 分钟将纬度和经度存储在 CSV 文件中

转载 作者:行者123 更新时间:2023-11-30 01:53:07 25 4
gpt4 key购买 nike

我想每 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

最佳答案

为了完成重复性任务,您可以使用 Handler,例如,

handler.postDelayed(new Runnable(){
@Override
public void run() {
// get Location or do whatever you want
}
}, (15 * 60 * 1000));

在这种情况下,getLocation() 将每 15 分钟调用一次。

关于android - 每 15 分钟将纬度和经度存储在 CSV 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32709372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com