Android中的ACCESS_MOCK_LOCATION权限使用Demo

转载地址: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 two serious limitations:

  1. DDMS sets location for GPS location provider only. If your application uses network provider then you are out of luck.
  2. DDMS can set location for an emulator device only. You can not do testing using a real device.

If you need to test with real device or using the network location provider, you will need to develop a mock provider within the application. A mock provider can represent any location provider – network or GPS. Writing a mock provider itself is easy. Just be careful about removing the feature before publishing your application.

In this article, we will see how to create a mock location provider.

First, we will develop a class that will encapsulate the details:

public class MockLocationProvider {
  String providerName;
  Context ctx;

  public MockLocationProvider(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 be helpful:

  • The constructor takes the name of the location provider that this mock provider will replace. For example, LocationManager.GPS_PROVIDER. The calls to the addTestProvider() and setTestProviderEnabled() tells the LocationManager that the given provider will 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 are 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 will get the mock location
      }
      //...
    };

    locMgr.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 1000, 1, lis);
  }

  protected void onDestroy() {
   mock.shutdown();
   super.onDestroy();
  }
}

Setting up Security

For mock location to work, certain permissions have to be set.

You will 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 will need to enable mock locations for the device using Settings > Developer options > Allow mock locations.

Disabling Mock Location

It is important that you hide the mock location provider business from the release build. A good way to do that is to enable mock location only if the application is 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.

Android中的ACCESS_MOCK_LOCATION权限使用Demo

时间: 2024-08-03 16:17:20

Android中的ACCESS_MOCK_LOCATION权限使用Demo的相关文章

Android中的文件权限操作

默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的文私有文件. 如果别的Android工程访问本工程的文件的话就会受限制, android的内核是linux, 所以他的文件管理和linux中的文件时一样的. 创建文件代码: /** * 创建各种文件 * @throws IOException * */ @SuppressLint({ "WorldW

android中获取root权限的方法以及原理(转)

一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有掌控手机系统的权限,比如删除一些system/app下面的无用软件,更换开关机铃声和动画,拦截状态栏弹出的广告等. 2. Root的原理介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限

Android 中运行时权限获取联系人信息 Demo

代码比较简单... AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="club.seliote.readcontact"> <uses-permission android:n

android 中使用自定义权限

1.如果在一个进程中启动另外一个进程的activity <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.weiyuan.com.hkkj"> <application android:allowBackup=

android 中使用自定义权限在广播中的利用

1.在一个进程中发送一个有自定义权限的广播,另外一个进程中拥有广播接受者接受到该广播 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.weiyuan.com.hkkj"> <application andro

Android中各个权限详解

在Android的设计中,资源的访问或者网络连接,要得到这些服务都需要声明其访问权限,否则将无法正常工作.在Android中这样的权限有很多种,这里将各类访问权限一一罗列出来,供大家使用时参考之用. android.permission.EXPAND_STATUS_BAR允许一个程序扩展收缩在状态栏,android开发网提示应该是一个类似Windows Mobile中的托盘程序 android.permission.FACTORY_TEST作为一个工厂测试程序,运行在root用户 android

Android中如何做到自定义的广播只能有指定的app接收

今天没吊事,又去面试了,具体哪家公司就不说了,因为我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目,好吧,我错了...其实当我们已经在工作的时候,我们可以在空闲的时间去面一面,因为面试有很多好处的: 第一点:你知道这个公司的具体地址了,以后和朋友说的时候也是有话题的 第二点:这点很重要,看看其他公司的面试题(现在有的公司还在采用笔试题这个环节,真心无语了,题目全是从网上找的,很没有意思,所以我只要见到有笔试题的一律pass,个人感觉面到现在,阿里和滴滴还是不错的,他

Android中那些权限

Permission Permission Permission Group Permission Tree Users Permission ACCESS_CHECKIN_PROPERTIES 允许读写访问"properties"表在checkin数据库中,改值可以修改上传( Allows read/write access to the "properties" table in the checkin database, to change values th

入门篇:11.Android中日志系统和权限系统

一.安卓中的日志系统 1.java中常用的两个日志 System.out.println();//普通日志 System.err.println();//警告日志 2.android中常用的日志种类 Log.e(Tag,"错误信息"); Log.w(Tag,"警告信息"); Log.i(Tag,"普通信息"); Log.d(Tag,"调试信息"); Log.v(Tag,"无用信息"); ps:这个log.v