Now the software market has a lot of software that can change the location of mobile phones, the changes can be opened to arbitrary positioning, and even a few years ago to rely on this technology advertising for a living small company.
A way to get a location
There are generally two ways to get a geographic location.
1) GPS
24 Satellite locations Worldwide (image from Wikipedia) give the detailed address: Http://zh.wikipedia.org/wiki/GPS;
But to the satellite demand for signal strength is relatively high, many specialized hardware equipment sometimes can not reach, so the use of GPS mobile phone 90% of the possible to obtain a null.
2) Network
This approach is in fact divided into two different situations. One is wired one for wireless.
1, Wired
Because the network location of the cable is fixed, the IP of each access point is fixed again, and the IP and location of the network generates one by one correspondence. Geo-location is available based on the IP of the access point. But the work seems to be done by a system or network provider, without the Android developer parsing it.
2, Wireless
Wireless is used for base station positioning. In fact, it's mathematically three-point positioning. For example, the mobile company, its base station all over the country, if a device wants to connect wireless network, nearby three base station will determine the visitor's relative distance according to the signal intensity, draw three radius is the distance of the garden, according to three circle intersection know the approximate geographical coordinates. (Image of Baidu Encyclopedia)
3,wifi
For example, some tablets, cannot plug in the network card, cannot insert the mobile card, they access the network is to walk the WiFi, then its geographical acquisition is based on the WiFi sharing device's geographical location to roughly determine. The location of the WiFi shared device is determined by the above 1, 2;
II: Get the location
Android offers a
Locationmanager class to manage geolocation information, which provides convenient features such as adding, deleting, filtering, and so on.
public class
Locationmanager extends Object
Java.lang.Object
?
Android.location.LocationManager
This object cannot be new and can only be obtained from the system
Class Overview
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an applicat Ion-specified when the Intent device enters the proximity of a given geographical location.
Instantiate this class directly; Instead, retrieve it through Context.getSystemService(Context.LOCATION_SERVICE) .
Unless noted, all location API methods require the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions. If your application only had the coarse permission then it is not having access to the GPS or passive location providers. Other providers would still return location results, but the update rate would be throttled and the exact location would be O Bfuscated to a coarse level of accuracy.
In addition, this object gets all the location providers, and deletes, adds, and so on.
Too much, do not copy, give the API link directly: http://developer.android.com/reference/android/location/LocationManager.html
It's much easier to get the current location after you know the class. There's a lot of code online.
It is roughly the acquisition of a location provider, which loops as each provider asks for geographic information, and the information is packaged into the class of locations.
You can call the location object after it gets
?
1
2
3
4
5
6
7
8
9
10
11
publicLocation getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location == null) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
returnlocation;
}
Why to get two times, because just said, generally get GPs get not, but actually get two times should also be null.
For Getlastkownlocation ()
Location
Getlastknownlocation (String provider) Returns A location indicating the data from the last known location fix obtained fro M the given provider.
He accepts a provider name parameter, typically if the system is called:
Gps_provider, Network_provider, these two easy to understand, one is the network one is the GPS in addition, there is a passive_provider
public static final String Passive_provider Added in API level 8
A special location provider for receiving locations without actually initiating a location fix.
This provider can is used to passively receive location updates if other applications or services request them without a Ctually requesting the locations yourself. This provider would return locations generated by other providers. You can query the method to determine the origin of the location getProvider() update. Requires the permission ACCESS_FINE_LOCATION , although if the GPS is not enabled this provider might only return coarse fixes.
Constant Value:passive This is what is still not understood.
But you can also manually get all the providers, because it is possible that the device has already added some location providers (the next part of the analysis to add location to the system, that is, virtual geographic location), I think most of the software is written or should be written like this, get all the location providers, and then loop to ask for locations:
List
Getproviders (Boolean enabledonly) Returns a list of the names of location providers.
List
Getproviders (Criteria, Boolean enabledonly) Returns a list of the names of locationproviders that satisfy the GIV En criteria, or null if none do.
With these two methods, for the first parameter of the second overloaded method, it means to restrict the conditional class, that is, to return a qualified name based on the provided restrictions on the location provider.
Once you get the string array, you can loop through the position:
?
1
2
3
4
5
6
7
8
9
10
11
privateLocation getLocation()
{
for(inti = 0; i < locProvider.length, i ++)
{
Location location = locationManager.getLocation(locProvider.get(i));
if(location!=null)
returnlocation;
}
returnnull;
}
In addition, the existing location provider can also be modified (the system's original three changes are not), but need
1, Get Locationprovider class
Locationprovider
Getprovider (String name) Returns The information associated with the location provider of the given name, or null if no PR Ovider exists by that name.
2, modify
3 Delete the original
void
Removetestprovider (String provider) removes the mock location provider with the given name.
4 Add the modified.
void
Addtestprovider (String name, Boolean requiresnetwork, Boolean Requiressatellite, Boolean Requirescell, Boolean Hasmonetarycost, Boolean supportsaltitude, Boolean supportsspeed, Boolean supportsbearing, int powerrequirement, int Accuracy) creates a mock location provider and adds it to the set of active providers.
Of course, to add the appropriate permissions:
Three virtual locations
In fact, Google Play has a ready-made program: Https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=zh_TW,
And there's managed source code on GitHub.
The method is to add a location provider that continuously passes Locaiton information to the location provider.
public void settestproviderenabled (String provider, Boolean enabled) Added in API level 3
Sets a mock enabled value for the given provider. This value would be used in place of any actual value from the provider.
Parameters
Provider
The provider name
Enabled
The mock enabled value
Throws
SecurityException
If the Access_mock_location permission is not present or the Settings.Secure.ALLOW_MOCK_LOCATION } system setting are not enabled
IllegalArgumentException
If no provider with the given name exists
This is also required to add special permissions:
In addition, the test found that it is better to direct the "GPs" location, other software is more acceptable, but requires users to open the virtual location option.