0

If I am using the LocationManager.NETWORK_PROVIDER is there a way to know if the location that triggered onLocationChanged came from a WiFi connection or from a cell tower connection?

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187

2 Answers2

2

You can ask with

public void onLocationChanged(Location location){
    String provider = location.getProvider(); 
}

or

    ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    //android.net.NetworkInfo
    NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


        if(wifi.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED)
            locationProvider = LocationManager.NETWORK_PROVIDER;
        else //check if it is a tablet without 3G connection
            if(mobile!=null){
                if(mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED)
                     locationProvider = LocationManager.PASSIVE_PROVIDER;
            }
ziniestro
  • 686
  • 1
  • 11
  • 24
  • Why is the PASSIVE_PROVIDER set here in case the mobile network is available? – Emil Adz Oct 30 '13 at 00:52
  • just in case, if in your app is most important to get connectivity from WIFI but it is not available, could get a connection to 3G. – ziniestro Oct 30 '13 at 09:05
1

Update: The below answer will only provide either GPS or Network as the provider. Here is a similar question that may be helpful. How to know the positioning mode is WIFI or 2G/3G cell tower in Android?

Are you using onLocationChanged method inside the LocationListener to get the coordinates? If so you can use the location.getProvider() method to get the provider.

LocationListner locationListner = new LocationListener(){
    public void onLocationChanged(Location location){
        String provider = location.getProvider(); 
    }
}
Community
  • 1
  • 1
bradley4
  • 3,823
  • 6
  • 34
  • 41
  • Are you sure I can use that? because as far as I seen the options there are "gps" and "network". – Emil Adz Oct 30 '13 at 00:12
  • You are right. My answer will only give you gps or network. "Kung Foo" thinks he as the answer.. http://stackoverflow.com/a/14558518/770061 – bradley4 Oct 30 '13 at 00:24
  • I will check the code in the other question and let you know. – Emil Adz Oct 30 '13 at 00:52
  • Well, it's looks like the code that was provided by "Kung Foo" is working but not on all devices I have tested it on. – Emil Adz Oct 30 '13 at 16:35