在Android中设备静止一段时间后,在GPS_PROVIDER和NET PROVIDER之间切换

时间:2014-10-23 05:11:07

标签: android gps

我正在编写GPS追踪器,我每分钟都会使用它来获取设备的位置。我使用GPS_PROVIDERNET_PROVIDER来获取位置,但有时候,即使我长时间移动设备,位置也不会更新。

所以我在想:

  

如果某个提供商的位置没有改变一段时间,请切换到其他提供商并再次检查新位置。

这是对的吗?

这是我的getLocation代码:

public Location getLocation(Context context) {
        this.mContext = context;
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled) {
                // no network provider is enabled
                this.canGetLocation = false;
                Log.d("DMCAILOCATION","DMCAILOCATION: can not get location");
            } else {
                this.canGetLocation = true;

                //=======================
                Location location=null;
                Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                Log.d("DMCAILOCATION","DMCAILOCATION: locationGPS:"+locationGPS + "locationNet:"+ locationNet  );
                if ( locationGPS != null ) {
                    location= locationGPS;
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    getLocationnet();
                    Log.d("DMCAILOCATION","DMCAILOCATION: get by gps");
                }
                else  {
                    location= locationNet;              locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    getLocationgps();   
                    Log.d("DMCAILOCATION","DMCAILOCATION: get by net");
                }
                Log.i("DMCAILOCATION","NetLocationTime:" + locationNet.getTime() + "locationGPS.getTime(): " +locationGPS.getTime());


                Log.d("DMCAILOCATION","DMCAILOCATION: LAT: "+latitude+", "+longitude);              
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

0 个答案:

没有答案