在服务中正确使用 GPS "Activate and disable automatically using the actual speed"
Use correctly GPS in a service "Activate and disable automatically using the actual speed"
在服务中:我想要的是GPS每10-15分钟只请求一次速度,如果实际捕获的速度高于X值,刷新时间从15分钟变为像 GPS 一样快。暂时我有这个:
public class MyService extends Service implements SensorEventListener,LocationListener {
@Override
public void onCreate() {
super.onCreate();
time = High_Refresh;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, this); //GPS_Provider
}
@Override
public void onLocationChanged(Location location) {
Actual_Speed = location.getSpeed();
if (Actual_Speed >= Speed_Value_Change) {
if (d == 0) {
enjegarAccelerometre();
}
d = De_Rapid_A_Lent;
time = High_Refresh;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, this);
//notificacioProva("2","3","4");
} else {
if (d == 0)
{
time = Low_Refresh;
pararAccelerometre();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, this);
}
else
{
d--;
}
}
Last_Speed=Actual_Speed;
}
}
但是使用这段代码,我认为 GPS 一直在运行 "The Position sensor apear always on the top"
我可以用其他方法做这个吗?
如果您需要 LocationManager return 只是一个 Location 对象,那么使用 requestSingleUpdate(),然后 onLocationChanged() 将被调用一次。
并且在一段时间内,当您想尽可能频繁地接收 GPS 速度时,
requestLocationUpdates() 是正确的方法。
当您不再需要获取位置数据时,请记住始终在 LocationManager 上调用 removeUpdates(listener),因为侦听更新会消耗大量电池。
在服务中:我想要的是GPS每10-15分钟只请求一次速度,如果实际捕获的速度高于X值,刷新时间从15分钟变为像 GPS 一样快。暂时我有这个:
public class MyService extends Service implements SensorEventListener,LocationListener {
@Override
public void onCreate() {
super.onCreate();
time = High_Refresh;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, this); //GPS_Provider
}
@Override
public void onLocationChanged(Location location) {
Actual_Speed = location.getSpeed();
if (Actual_Speed >= Speed_Value_Change) {
if (d == 0) {
enjegarAccelerometre();
}
d = De_Rapid_A_Lent;
time = High_Refresh;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, this);
//notificacioProva("2","3","4");
} else {
if (d == 0)
{
time = Low_Refresh;
pararAccelerometre();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, this);
}
else
{
d--;
}
}
Last_Speed=Actual_Speed;
}
}
但是使用这段代码,我认为 GPS 一直在运行 "The Position sensor apear always on the top"
我可以用其他方法做这个吗?
如果您需要 LocationManager return 只是一个 Location 对象,那么使用 requestSingleUpdate(),然后 onLocationChanged() 将被调用一次。
并且在一段时间内,当您想尽可能频繁地接收 GPS 速度时, requestLocationUpdates() 是正确的方法。
当您不再需要获取位置数据时,请记住始终在 LocationManager 上调用 removeUpdates(listener),因为侦听更新会消耗大量电池。