Access_mock_location Permissions in Android use Demo

Source: Internet
Author: User

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:

    1. DDMS sets location for GPS location provider only. IF your application uses network provider then your is out of luck.
    2. 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.

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.