Вопрос

I am developing an android app which will retrieve data every 30sec and send to the server, so mobile internet is necessary.

currently, I am using GPS but I also want to use network provider (wifi and cell tower's).

But my question is how does android retrieve location info through wifi? If the location can be retrieved from wifi then can it be retrieved from my mobile internet itself?

If no then why? If yes then show me some code snippets to retrieve location update from mobile internet and also what can be the approx accuracy? I need accuracy to be within 100meters.

I followed these and got this doubt http://developer.android.com/guide/topics/location/strategies.html Is android application able to retrieve location info from connected WiFi hotspot AP?

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

Решение

To retrieve the location from a NETWORK_PROVIDER you can use the following:

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerGps);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationManager lm;
if(lm==null)
  lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

This is however a basic example, so to distinguish between the wi-fi and the 3g network first take a look here and before the code above do:

One certain way of obtaining what you want is the following:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

State mobile = conMan.getNetworkInfo(0).getState();
State wifi = conMan.getNetworkInfo(1).getState();

After that, you distinguish them by:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
    //mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
    //wifi
}

Check, if the mobile or wifi is connected, if wifi - then use PASSIVE_PROVIDER, if mobile - use NETWORK_PROVIDER.

I hope all of this information is useful for you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top