如何在后台每 30 分钟获取一次 GPS 位置
我正在尝试制作一个应用程序,每隔 x 分钟将您的 GPS 位置发送到网络服务器。我很困惑我应该如何做到这一点。我是否使用计时器或不同的服务每 x 分钟调用一次 get gps 函数?
这就是我用来获取 GPS 信号的
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = locationManager.getBestProvider(criteria,
false);
LocationListener loc_listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
try {
Looper.prepare();
locationManager.requestLocationUpdates(bestProvider, 0, 0,
loc_listener);
} catch (Exception e) {
e.printStackTrace();
}
Location location = locationManager
.getLastKnownLocation(bestProvider);
I am trying to make an app that will send your gps location to a webserver every x mins. Im confused about how I should do this. Do I use a timer or a different service to call the get gps function every x mins?
This is what I'm using to get the gps signal
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = locationManager.getBestProvider(criteria,
false);
LocationListener loc_listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
try {
Looper.prepare();
locationManager.requestLocationUpdates(bestProvider, 0, 0,
loc_listener);
} catch (Exception e) {
e.printStackTrace();
}
Location location = locationManager
.getLastKnownLocation(bestProvider);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
闹钟 非常适合此目的。您可以让应用程序请求系统每 30 分钟触发一次警报(每次手动或使用 setRepeating() 方法)。无论您的应用程序当时是否正在运行,该警报都会响起。您将需要一个广播接收器来处理它生成的意图。 这里有一个关于警报的很好的教程。
An alarm is perfect for this. You can have your application request the system to set off the alarm every 30 minutes (either manually each time or with the setRepeating() method). This alarm will go off regardless of whether your application is running at the time or not. You will need a broadcast receiver to handle the intent it generates. There is a good tutorial on alarms here.
您应该注册到 AlarmManger 来启动您的 IntentService 它将获取位置并将其发送到服务器。
在服务执行结束时,设置一个新的警报,时间为当前时间后 30 分钟。
You should register to the AlarmManger to start your IntentService which will get the position and send it to the server.
At the end of your service execution, set a new Alarm for 30 minutes from current time.