我可以将位置与WiFi和Cell Tower区分开来吗?

时间:2013-10-29 23:55:52

标签: android location wifi locationmanager

如果我使用LocationManager.NETWORK_PROVIDER,是否有办法知道触发onLocationChanged的位置是来自WiFi连接还是来自手机信号塔连接?

2 个答案:

答案 0 :(得分:2)

你可以问

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

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

答案 1 :(得分:1)

更新:以下答案仅提供GPS或网络作为提供商。这是一个类似的问题,可能会有所帮助。 How to know the positioning mode is WIFI or 2G/3G cell tower in Android?

您是否在LocationListener中使用onLocationChanged方法来获取坐标?如果是这样,您可以使用location.getProvider()方法获取提供程序。

LocationListner locationListner = new LocationListener(){
    public void onLocationChanged(Location location){
        String provider = location.getProvider(); 
    }
}