Android Tower Location

Below is my code:

private Location getLastKnownLoaction(boolean enabledProvidersOnly){

    LocationManager manager = mActivityContext.getSystemService(Context.LOCATION_SERVICE);
    Location location = null;
    List<String> providers = manager.getProviders(enabledProvidersOnly)

    for(String provider : providers){
        location = manager.getLastKnownLocation(provider);

        //maybe try adding some Criteria here
        if(location != null) return location;
    }

    //at this point we've done all we can and no location is returned
    return null;
}

I saw the location of the tower displayed on old Nokia phones. How to implement similar functions in Android? "

+4
source share
2 answers

Make sure you have it on your manifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

And also you need to try it on a real device, not on a simulator.

+1
source

It is not possible to get only the location of the tower in android. You can do the following:

In the manifest, add permission:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

and in your code use

Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

This will help you get the network location. If his course, he will most likely provide you with a Tower-based location.

, , .

+1

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


All Articles