onlocationchanged 未被调用(延迟参数不适用)

onlocationchanged is not called (delay parameter does not apply)

locationManager.requestLocationUpdates(provider, time, distance, locationListener);

我知道没有时间和距离的优先级,只要满足其中一个条件就叫一个onLocationChanged()

但是onLocationChanged()没有调用,只调用了这种情况。

1. Load the page and turns on the gps.
2. location listener catch (0,0) white gps is turned on. (onLocationChanged called)
3. gps is turn on finish.
4. The location listener is called after the time that was 'time' parameter of requestLocationUpdates(). (onLocationChanged called)
5. After... onLocationChanged  is not called never..

1. If the beginning location is normal value, not (0,0). onLocationChanged  is not ever call. (Even after the time specified in the parameters)

为什么不适用于requestLocationUpdates()的时间参数?

这是我的代码。

private BroadcastReceiver gpsReceiver = new BroadcastReceiver() {

    private final String GET_GPS = "getGPS";
    private final String SET_GPS = "setGPS";
    private final String START_GPS = "startGPS";
    private final String FINISH_GPS = "finishGPS";


    Location location;
    LocationManager locationManager;
    double lat;
    double lon;
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
    private static final long MIN_TIME_BW_UPDATES = 1000 *60;
    boolean isGpsMode = false;

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getStringExtra("action");
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        JSONObject obj = new JSONObject();

        if(action.equals(GET_GPS)){
            if(isGPSOn()){
                try {
                    obj.put("result","gpsOn");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else{
                try {
                    obj.put("result","gpsOff");
                } catch (JSONException e) {
                    e.printStackTrace();
                }                   
            }
        }else if(action.equals(SET_GPS)){
            Intent settingIntent= new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            settingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(settingIntent);
        }else if(action.equals(START_GPS)){
            Log.d("GPS", "GPS Start");
            //stopUsingGPS();
            startCallback();
        }else if(action.equals(FINISH_GPS)){
            Log.d("GPS", "GPS Finish");
            stopUsingGPS();
        }

        if(!(obj.toString().equals("{}"))){
            sendScript("javascript:getGPS" + "(" + obj + ")");  
        }
    }


    public boolean isGPSOn(){
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }


    public boolean isNetworkOn(){
        return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }


    public void stopUsingGPS(){
        Log.d("GPS", "stopUsingGPS()");
        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
            Log.d("GPS", "lovationManager remove");
        }
    }


    public void startCallback(){
        Log.d("GPS", "startCallback()");
        if(isGpsMode != isGPSOn()){
            if(isGpsMode){
                if(isNetworkOn()){
                    NetworkRegistration();
                }
            }else{
                if(!GPSRegistration()){
                    if(isNetworkOn()){
                        NetworkRegistration();
                    }
                }
            }
        }
    }


    public boolean GPSRegistration(){
        Log.d("GPS", "call location value from GPS");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
        saveLocation();
        isGpsMode = true;
        Log.d("GPS", "lat: " + String.valueOf(lat)+" / lon: " + String.valueOf(lon));

        return isAvailableLocation(lat, lon);
    }

    public void NetworkRegistration(){
        Log.d("GPS", "call location value from Network");
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
        saveLocation();
        isGpsMode = false;
        Log.d("GPS", "lat: " + String.valueOf(lat)+" / lon : " + String.valueOf(lon));
    }

    public void saveLocation(){
        location = null;
        if (locationManager != null) {
            location = getLastKnownLocation();
            if (location != null) {
                lat = location.getLatitude();
                lon = location.getLongitude();
            }
        }
    }

    private Location getLastKnownLocation() {
        List<String> providers = locationManager.getProviders(true);
        Location bestLocation = null;
        for (String provider : providers) {
            Location l = locationManager.getLastKnownLocation(provider);
            if (l == null) {
                continue;
            }
            if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                bestLocation = l;
            }
        }
        return bestLocation;
    }

    public boolean isAvailableLocation(double lat, double lon){
        if(lon>124 && lon<132 && lat>33 && lat<43){
            return true;
        }
        return false;
    }

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("GPS", "onLocationChanged - lat: "+lat+" / lon: "+lon);

            if(!(isAvailableLocation(lat, lon))){
                stopUsingGPS();

                if(isGpsMode){ 
                    if(isNetworkOn()){
                        NetworkRegistration();
                        if(isAvailableLocation(lat, lon)){
                            isGpsMode = !isGpsMode;
                        }
                    }
                }else{
                    if(isGPSOn()){
                        GPSRegistration();
                        if(isAvailableLocation(lat, lon)){
                            isGpsMode = !isGpsMode;
                        }
                    }
                }
            }

            if(isAvailableLocation(lat, lon)){
                JSONObject obj = new JSONObject();
                try{
                    obj.put("lat", lat);
                    obj.put("lon", lon);
                }catch( JSONException e){
                    Log.d("GPS", "onRegistered: JSON exception");
                }
                sendScript("javascript:sendGPS("+obj+");");
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    };
};

我之所以这样编码,一定是在 Cordova 中 Activity。 (而且老板想要...)

+) 我故意删了评论,因为评论是韩文的..(我是韩国人) 对不起,很难理解,因为没有评论... :(

全部,谢谢:)

我认为您应该为 GPS 使用 Service 而不是 BroadcastReceiver。

自己回答。

1. onlocationchanged 未被调用

在室内使用 GPS 提供商时会出现这种情况。 在房间使用 GPS 提供商时,不会调用 onLocationChange ()。 (网络提供商运营非常成功。) 所以,gps 提供者的值是无效的,必须连接到网络提供者。

2。 requestLocationUpdates() 的时间参数不适用

虽然不准确,但我觉得这大概和时间长短有关。 因为,我在5分钟(1000 * 60 * 5 ms).

时确认是工作的

短于一定时间则运行不正常。 我觉得一定周期是一分钟(1000 * 6 ms).

如果你需要,这是我的代码。

private BroadcastReceiver gpsReceiver = new BroadcastReceiver() {

    private final String GET_GPS = "getGPS";
    private final String SET_GPS = "setGPS";
    private final String START_GPS = "startGPS";
    private final String FINISH_GPS = "finishGPS";


    LocationManager locationManager;
    double lat;
    double lon;
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 5;
    boolean isGpsMode = false;

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getStringExtra("action");
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        JSONObject obj = new JSONObject();

        if(action.equals(GET_GPS)){
            if(isGPSOn()){
                try {
                    obj.put("result","gpsOn");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else{
                try {
                    obj.put("result","gpsOff");
                } catch (JSONException e) {
                    e.printStackTrace();
                }                   
            }
        }else if(action.equals(SET_GPS)){
            Intent settingIntent= new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            settingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(settingIntent);
        }else if(action.equals(START_GPS)){
            Log.d("GPS", "GPS start");
            startCallback();
        }else if(action.equals(FINISH_GPS)){
            Log.d("GPS", "GPS finish");
            stopUsingGPS();
        }

        if(!(obj.toString().equals("{}"))){
            sendScript("javascript:getGPS" + "(" + obj + ")");  
        }
    }

    public boolean isGPSOn(){
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    public boolean isNetworkOn(){
        return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

    public void stopUsingGPS(){
        Log.d("GPS", "stopUsingGPS()");
        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
        }
    }

    public void startCallback(){
        if(isGPSOn()){
            GPSRegistration();
        } else if(isNetworkOn()){
            NetworkRegistration();
        }
    }

    public boolean GPSRegistration(){
        Log.d("GPS", "call location value from GPS");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
        isGpsMode = true;
        Log.d("GPS", "lat: " + String.valueOf(lat)+" / lon: " + String.valueOf(lon));

        return isAvailableLocation(lat, lon);
    }

    public void NetworkRegistration(){
        Log.d("GPS", "call location value from Network");
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
        isGpsMode = false;
        Log.d("GPS", "lat: " + String.valueOf(lat)+" / lon: " + String.valueOf(lon));
    }

    public boolean isAvailableLocation(double lat, double lon){
        if(lon>124 && lon<132 && lat>33 && lat<43){
            return true;
        }
        return false;
    }

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                lat = location.getLatitude();
                lon = location.getLongitude();
            }

            Log.d("GPS", "onLocationChanged - lat: "+lat+" / lon : "+lon);

            if (isAvailableLocation(lat, lon)) {
                JSONObject obj = new JSONObject();
                try{
                    obj.put("lat", lat);
                    obj.put("lon", lon);
                }catch( JSONException e){
                    Log.d("GPS", "onRegistered: JSON exception");
                }
                sendScript("javascript:sendGPS("+obj+");");
            } else {
                stopUsingGPS();

                if(isGpsMode){ 
                    if(isNetworkOn()){
                        NetworkRegistration();
                    }
                }else{
                    if(isGPSOn()){
                        GPSRegistration();
                    }
                }
                return;
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    };
};

我已经在 onCreate() 中编写了这段代码,

registerReceiver(gpsReceiver, new IntentFilter("actionName"));

这段代码在 onDestroy() 中。

unregisterReceiver(gpsReceiver);

然后,可通过

startIntent(actionName).