调用“removeUpdates”后 GPS 系统不会重新启动

发布于 2024-11-29 17:35:04 字数 2633 浏览 3 评论 0原文

在我的应用程序中,我在 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);

In my application I call "requestLocationUpdates" in onStart() method and I receive updates properly.

When I turn the GPS off by calling "removeUpdate(locationlistener)" I stop receiving updates as expected.

The problem is that when I want to restart the GPS system by calling "requestLocationUpdates" method again it isn't working! It never enters the "onLocationChanged" or "onStatusChanged" methods

my gpsLocationListener code;

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;

    }
};

my removeupdates code;

if( ! deviceSettings.isprogramModeActive ){

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

my request again code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-12-06 17:35:04

我解决问题!

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

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);
        }
    };

I solve the problem!

I crate an call a handler from thread.

locateHandler.sendEmptyMessage(1);

I call the following code from a handler, not in the thread.

 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);
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文