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

时间:2015-01-23 09:55:19

标签: android gps location

in

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中。 (老板想要它......)

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

全部,谢谢:)

2 个答案:

答案 0 :(得分:0)

我认为您应该使用Service代替BroadcastReceiver进行GPS。

答案 1 :(得分:0)

自我回答。

<强> 1。 onlocationchanged未被称为

在室内使用GPS提供商时会显示此情况。 在房间使用GPS提供商时,onLocationChange()不会被调用。 (网络提供商运营非常成功。) 因此,gps提供程序的值无效,必须连接到网络提供程序。

<强> 2。 requestLocationUpdates()的Time参数不适用

虽然不准确,但我认为这可能与时间长短有关。 因为,我确认在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).