Removable network and GPS providers in accordance with android gps status

I would appreciate your help if anyone can help me. I need to have an accurate location tracker in my application, and I want it to act like that. he finds the first location of the person with the network, meanwhile I start to request GPS localization. when gps gives me the location, I want to no longer listen to the network. After that, I want to request a location only from the network, and only if GPS is not fixed (I can not tell me the location). When the GPS is fixed again, I want to stop listening to the network, and this is the cycle that I want to implement, since the GPS is clearer, and I want the network localization to be backup. I saw this post, but I canโ€™t figure out how to adapt it to my needs,

How to check the current status of the GPS receiver?

Ideas are needed, please in advance.

+6
source share
1 answer

1. Stop listening to network location updates: This is very simple. Just call

your_loc_manager.removeUpdates(network_location_listener); 

2. Obtaining a location from the network provider if the GPS provider does not provide any corrections. You can try something like the following (instead of a timer, you can try using the GpsStatus Listener ...)

inside the method of receiving network provider updates

 boolean network_enabled ; try { network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(Exception ex) { ex.printStackTrace(); } if(!network_enabled) return; locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, networkLocationListener); return; 

Inside GPS Location Method

 boolean gps_enabled; try { gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception ex) { ex.printStackTrace(); } if(!gps_enabled) { // call your method for getting network location updates return; } locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 50, locationListenerGps); new Timer().schedule(new TimerTask() { @Override public void run() { // You should call your method of getting Network locations based on some // condition which would tell whether you really need to ask for network location } }, 20000 /* Time Value after which you want to stop trying for GPS and switch to Network*/); 

EDIT 1:

Also "I donโ€™t need to know when to receive updates from GPS, I need to know when to receive updates from the network (I mean, I need to know when gps is looking, and not when it is fixed) and how to control interchangeability between the network and GPS "

... If the fix was lost, you only need to look for network updates, so I suggested calling

 if (hasGPSFix) { Log.i("GPS","Fix Lost (expired)"); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener); //===> new line which I am suggesting } hasGPSFix = false; 

Thus, you will receive a location update from the network, while GPS will continue to attempt corrections. After reaching the GPS installation, hasGPSFix will change its value and the existing code in the specified link will stop searching for network updates. At least this is something that I could understand what you want to achieve. Perhaps I didnโ€™t completely understand you.

EDIT 2: Here is my class for working with GPS and NETWORK providers.

In MapActivity, or where you need to get location updates, you need to instantiate this MyLocation class. And also, in order to receive location updates, you need to implement LocationResult in your call activity. public abstract void gotLocation(Location location, boolean isGpsData); will notify you if the update you received now appears from the network or GPS. In addition, in case of returning data from GPS, it automatically closes the netowrk receiver. You will need to call the getFineLocation() method if you need data from GPS, etc. You will need to improve this code for your purpose (for example, you need to improve the closeListeners() method to close only one provider, when you just want to remove only network updates, etc.), but it gives the basic implementation that you you want (along with the implementation of GpsStatus.Listener , of course, so both combined should serve your purpose well).

 public class MyLocation { LocationManager locManager; LocationResult locationResult; boolean gps_enabled = false; boolean network_enabled = false; public boolean getCoarseLocation(Context context, LocationResult result) { locationResult = result; if(locManager == null) locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try { network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(Exception ex) { ex.printStackTrace(); } if(!network_enabled) return false; locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, networkLocationListener); return true; } public boolean getFineLocation(Context context, LocationResult result) { locationResult = result; if(locManager == null) locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try { gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception ex) { } if(!gps_enabled) return false;//No way to get location data locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, locationListenerGps); return true; } LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location,true); locManager.removeUpdates(networkLocationListener); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {} }; LocationListener networkLocationListener = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location,false); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {} }; public void closeListeners() { if(locManager == null) return; if(locationListenerGps != null) locManager.removeUpdates(locationListenerGps); if(networkLocationListener != null) locManager.removeUpdates(networkLocationListener); } public static abstract class LocationResult { public abstract void gotLocation(Location location, boolean isGpsData); } public static boolean hasNetworkProvider(Context mContext) { List<String> myProvidersList = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getProviders(false); return myProvidersList.contains("network"); } public static boolean hasGPSProvider(Context mContext) { List<String> myProvidersList = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getProviders(false); return myProvidersList.contains("gps"); } } 
+1
source

Source: https://habr.com/ru/post/949472/


All Articles