Вопрос

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?

Это было полезно?

Решение 2

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(); 
    }
}

Другие советы

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;
            }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top