Android mock location

Source: Internet
Author: User

There are a lot of software in the software market that can change the geographic location of mobile phones. After the software is changed, it can be positioned at will. Even a few years ago, there were small companies that relied on this technology to make advertisements.

1. Obtain the geographic location

You can obtain a geographical location in two ways.

1) GPS

24 Global Satellite Positioning (picture from Wikipedia): http://zh.wikipedia.org/wiki/GPS;

However, satellite requests require high signal strength, and many specialized hardware devices sometimes fail to meet the requirement. Therefore, 90% of mobile phones may obtain null using GPS.

2) network

This method is actually divided into two situations. Either wired or wireless.

1, wired

Because the wired network is fixed and the ip address of each access point is fixed, the ip address and location of the network have a one-to-one correspondence relationship. You can obtain the geographic location based on the ip address of the Access Point. However, such work seems to be done by the system or network provider, and does not need to be resolved by android Developers.

2. Wireless

Wireless Location is used by the base station. It is actually three points in mathematics. For example, a mobile company uses its base stations all over the country. If a device wants to connect to a wireless network, the three nearby base stations will determine the relative distance of visitors based on the signal strength, draw three gardens with a distance and obtain the approximate geographical coordinates based on the intersection of the three circles. (Picture from Baidu encyclopedia)

3. wifi

For example, some tablet computers cannot be inserted with NICs or mobile phone cards, and they access the network through wifi, therefore, the geographic location acquisition is roughly determined based on the geographical location of the wifi sharing device. For details about how to determine the location of a Wi-Fi shared device, refer to the above 1, 2;

2. Obtain the geographic location

Android provides

The locationmanager class is used to manage geographical location information. It provides convenient functions such as adding, deleting, and filtering.

 

Public classLocationManagerextends 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 application-specifiedIntentWhen the device enters the proximity of a given geographical location.

You do not instantiate this class directly; instead, retrieve it throughContext.getSystemService(Context.LOCATION_SERVICE).

Unless noted, all Location API methods requireACCESS_COARSE_LOCATIONOrACCESS_FINE_LOCATIONPermissions. if your application only has the coarse permission then it will not have access to the GPS or passive location providers. other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.

In addition, this object obtains all location providers and deletes, adds, and other operations.

Too much, do not copy, directly give the api link is good: http://developer.android.com/reference/android/location/LocationManager.html

It is much easier to get the current geographic location after learning about this class. There are a lot of code on the Internet.

Generally, it is to obtain the Location provider. The cycle is like that every provider asks for geographical information. The information is packaged into the Location class by the system.

After obtaining the Location object, you can call

 

public Location 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);}return location;}

Why do we need to obtain the GPS twice? As we mentioned earlier, GPS is generally not obtained, but the retrieval is actually null.

 

For getLastKownLocation ()

 

Location GetLastKnownLocation (String provider) Returns a Location indicating the data from the last known location fix obtained from the given provider.
It accepts the parameters of a provider name. Generally, if the system is called:

 

GPS_PROVIDER and NETWORK_PROVIDER are easy to understand. One is the network, the other is gps, and the other is PASSIVE_PROVIDER.

 

Public static final String PASSIVE_PROVIDERAdded in API level 8

A special location provider for processing ing locations without actually initiating a location fix.

This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. You can querygetProvider()Method to determine the origin of the location update. Requires the permissionACCESS_FINE_LOCATION, Although if the GPS is not enabled this provider might only return coarse fixes.

Constant Value: passive.

 

However, you can also manually obtain all the providers, because it is possible that some location providers have been added to the local device (the next part of the analysis is added to the system, that is, virtual geographic location ), I think most software is written in this way or should be written in this way, get all location providers, and then repeatedly ask for location:

 

List GetProviders (boolean enabledOnly) Returns a list of the names of location providers.
List GetProviders (Criteria criteria, boolean enabledOnly) Returns a list of the names of LocationProviders that satisfy the given criteria, or null if none do.
The two methods are used. For the first parameter of the second overload method, it means the restriction condition class, that is, the qualified name is returned according to the provided restriction on the location provider.

 

After obtaining the String array, you can obtain the position cyclically:

 

 

 

private Location getLocation(){for(int i = 0; i < locProvider.length, i ++){Location location = locationManager.getLocation(locProvider.get(i));if(location!=null)return location;}return null;}

In addition, you can modify the existing location provider (the original three of the system cannot be modified ).

 

1. Obtain the LocationProvider class.

 

LocationProvider GetProvider (String name) Returns the information associated with the location provider of the given name, or null if no provider 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 one.

 

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, you must add the corresponding permissions:

 

 

3. Virtual geographic location

In fact, there is a ready-made program on google play: https://play.google.com/store/apps/details? Id = com. lexa. fakegps & hl = zh_TW,

In addition, github has hosted source code.

The specific method is to add a location provider and continuously transmit the 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 will 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 orSettings.Secure.ALLOW_MOCK_LOCATION} System setting is not enabled
IllegalArgumentException If no provider with the given name exists
This requires special permissions:

 

In addition, tests have found that it is better to directly transfer the location to the "gps". Other software is more acceptable, but you need to enable the virtual geographic location option.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.