gpt4 book ai didi

android - 调用 "removeUpdates"后 GPS 系统不会重新启动

转载 作者:太空狗 更新时间:2023-10-29 14:30:52 26 4
gpt4 key购买 nike

在我的应用程序中,我在 onStart() 方法中调用“requestLocationUpdates”,并且我正确地接收了更新。

当我通过调用“removeUpdate(locationlistener)”关闭 GPS 时,我不再按预期接收更新。

问题是,当我想通过再次调用“requestLocationUpdates”方法重新启动 GPS 系统时,它不起作用了!它从不进入“onLocationChanged”或“onStatusChanged”方法

我的 gpsLocationListener 代码;

public final LocationListener gpsLocationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Logging.TraceMessage("GPS available again", Logging.INFORMATION, Logging.MAINACTIVITY);
break;
case LocationProvider.OUT_OF_SERVICE:
Logging.TraceMessage("GPS out of service", Logging.INFORMATION, Logging.MAINACTIVITY);
deviceSettings.gpsStatus = "2";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Logging.TraceMessage("GPS temporarily unavailable", Logging.INFORMATION, Logging.MAINACTIVITY);
break;
}
}

public void onProviderEnabled(String provider) {
Logging.TraceMessage("GPS Provider Enabled", Logging.INFORMATION, Logging.MAINACTIVITY);
}

public void onProviderDisabled(String provider) {
Logging.TraceMessage("GPS Provider Disabled", Logging.INFORMATION, Logging.MAINACTIVITY);
deviceSettings.gpsStatus = "2";
}

public void onLocationChanged(Location location) {
if(location == null) return;

tempLoc.setItem(location.getLongitude(), location.getLatitude(), deviceSettings.getCurrentTime(), deviceSettings.satelliteCount, location.getSpeed());
if (!SafeLocation.IsSafe(tempLoc) ){
return;
}

deviceSettings.currX = tempLoc._x;
deviceSettings.currY = tempLoc._y;
deviceSettings.currSpeed = tempLoc._speed;
deviceSettings.currValid = true;

}
};

我的删除更新代码;

if( ! deviceSettings.isprogramModeActive ){

if(gpsLocationListener != null){
try {
locationManager.removeUpdates(gpsLocationListener);
} catch (Exception e) {
}
}
mylocatewait(30000);//30 saniye bekle
continue;
}

我的再次请求代码:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ServerSettings.getGpsPer()*1000, 0, gpsLocationListener);

最佳答案

我来解决问题!

我从线程创建一个调用处理程序。

locateHandler.sendEmptyMessage(1);

我从处理程序中调用以下代码,而不是在线程中。

 Handler locateHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 0)
locationManager.removeUpdates(gpsLocationListener);
else if(msg.what == 1)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ServerSettings.getGpsPer()*1000, 0, gpsLocationListener);
}
};

关于android - 调用 "removeUpdates"后 GPS 系统不会重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7080453/

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