3

私のアプリケーションでは、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;

    }
};

私のremoveupdatesコード;

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

1 に答える 1

1

私は問題を解決します!

スレッドからハンドラーの呼び出しを作成します。

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);
        }
    };
于 2011-08-17T12:15:05.130 に答える