Reprint Address: Http://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/
The DDMS tool can be used to push out test location during testing. However, it has the serious limitations:
- DDMS sets location for GPS location provider only. IF your application uses network provider then your is out of luck.
- DDMS can set location for a emulator device only. You can testing using a real device.
If you need to test with real device or using the network location provider, you'll need to develop a mock provider with In the application. A mock provider can represent any location provider–network or GPS. Writing a mock provider itself is easy. Just is careful about removing the feature before publishing your application.
In this article, we'll see how to create a mock location provider.
First, we'll develop a class that would encapsulate the details:
public classMocklocationprovider {String providerName; Context CTX; PublicMocklocationprovider (String name, Context ctx) {this.providername = name; This.ctx = ctx; Locationmanager lm = (Locationmanager) Ctx.getsystemservice (Context.location_service); Lm.addtestprovider (ProviderName, False, False, False, False, False , True, True, 0, 5 ); Lm.settestproviderenabled (ProviderName, True ), public void pushlocation (double lat, double lon) { Locationmanager lm = (Locationmanager) Ctx.getsystemservice (Context.location_service); Location mocklocation = new location (providerName); mocklocation.setlatitude (LAT); Mocklocation.setlongitude ( LON); Mocklocation.setaltitude (0 ); Mocklocation.settime (System.currenttimemillis ()); Lm.settestproviderlocation ( ProviderName, mocklocation); } public void shutdown () {Locationmanager lm = (Locationmanager) Ctx.getsystemservice (context.location_ SERVICE); Lm.removetestprovider (ProviderName); }}
A brief description of the Mocklocationprovider class may helpful:
- The constructor takes the name of the the location provider, the This mock provider would replace. For example, Locationmanager.gps_provider. The calls to the Addtestprovider () and settestproviderenabled () tells the Locationmanager that the given provider would be Replaced by mock data.
- The Pushlocation () method supplies mock location data for a given provider.
Any activity or service can easily use the class. Here, we is replacing the network provider with a mock one.
public class Mainactivity extends Activity {mocklocationprovider mock; public void onCreate (Bundle savedinstancestate) {Super . OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); mock = new Mocklocationprovider (Locationmanager.network_provider, this ); Set Test Location Mock.pushlocation ( -12.34, 23.45 ); Locationmanager locmgr = (Locationmanager) Getsystemservice (Location_service); Locationlistener lis = new Locationlistener () {public void onlocationchanged (location location) {//you would Get the mock location }//... }; Locmgr.requestlocationupdates (Locationmanager.network_provider, +, 1 , LIS);} protected void OnDestroy () {Mock.shutdown (); Super . OnDestroy ();}}
Setting up Security
For a mock location-to-work, certain permissions has to be set.
You'll need to request the Android.permission.ACCESS_MOCK_LOCATION permission, in addition to any other permission.
<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android: Name= "Android.permission.ACCESS_MOCK_LOCATION"/>
Finally, you'll need to enable a mock locations for the device using Settings > Developer options > Allow mock Locat Ions.
Disabling Mock location
It is important, you hide, the mock location provider business from the release build. A good A-to-do-is-to-enable mock location only if the application are being run in debug mode. In your code, check if Debuggable flag is set:
if (Getapplication (). Getapplicationinfo (). Flags & applicationinfo.flag_debuggable)! = 0) { mock = new Mocklocationprovider ( locationmanager.network_provider, this); Set Test Location mock.pushlocation ( -12.34, 23.45);}
when you test the app using USB debugging or using the emulator, the debug flag is set automatically.