转载地址: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的更多相关文章

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

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...

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

    代码比较简单... AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <m ...

  3. Android中的文件权限操作

    默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...

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

    1.在一个进程中发送一个有自定义权限的广播,另外一个进程中拥有广播接受者接受到该广播 <?xml version="1.0" encoding="utf-8&quo ...

  5. android 中使用自定义权限

    1.如果在一个进程中启动另外一个进程的activity <?xml version="1.0" encoding="utf-8"?> <man ...

  6. 快速解决Android中的selinux权限问题【转】

    本文转载自:http://blog.csdn.net/mike8825/article/details/49428417 版权声明:本文为博主原创文章,未经博主允许不得转载. 关于selinux的详细 ...

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

    今天没吊事,又去面试了,具体哪家公司就不说了,因为我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目,好吧,我错了...其实当我们已经在工作的时候,我们可以在空闲的时间去面 ...

  8. Android中的各种访问权限Permission含义

    android.permission.EXPAND_STATUS_BAR 允许一个程序扩展收缩在状态栏,android开发网提示应该是一个类似Windows Mobile中的托盘程序 android. ...

  9. Android中的WebView进行直接加载网页(要注意解决权限问题)

    我们都知道Android的网络功能很不错,当然Android中WebView组件也挺不错,可以直接进行加载网页,我们可以把这个看做一个小型的浏览器\ [注]以下的一些内容我翻译了一下文档,可能有些翻译 ...

随机推荐

  1. VUE v-bind绑定class和style

    1.绑定class (1)对象语法 <!DOCTYPE html> <html lang="zh"> <head> <meta chars ...

  2. 【DB2】DbVisualizer编译存储过程

    之前我一直以为DbVisualizer是不可以编译存储过程的,现在才发现是可以的,编译如下: 只需要在编译的时候注意使用--/与/将存储过程包为起来编辑即可.

  3. python内置函数、匿名函数、递归

    python3--内置函数 内置函数: 截止到python 3.6.2 版本,现在python一共提供了68个内置函数:即python提供给你直接可以拿来使用的所有函数.   内置函数  (点击函数查 ...

  4. Javascript 中使用Json的四种途径

    1.jQuery插件支持的转换方式: 复制代码代码如下: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 ...

  5. SpringCloud系列十七:Hystrix的监控

    1. 回顾 上文讲解了使用Hystrix为Feign添加回退,并通过Fallback Factory检查回退原因以及如何为Feign客户端禁用Hystrix. 2. Hystrix的监控 除实现容错外 ...

  6. Redis(三):windows下Redis的安装配置以及注意事项

    一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...

  7. Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条)

    Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条) 本篇博客介绍Cocos2d-x中的进度条动画,进度条涉及以下几个重要的类和方法,笔者来给大家具体解说一 ...

  8. atitit.词法分析原理 词法分析器 (Lexer)

    atitit.词法分析原理 词法分析器 (Lexer) 1. 词法分析(英语:lexical analysis)1 2. :实现词法分析程序的常用途径:自动生成,手工生成.[1] 2 2.1. 词法分 ...

  9. Python学习笔记7:函数对象及函数对象作參数

    一.lambda函数 比如: fun1 = lambda x,y: x + y print fun1(3,4) 输出:7 lambda生成一个函数对象.该函数參数为x,y,返回值为x+y.函数对象赋给 ...

  10. zookeeper伪分布式安装

    本文介绍zookeeper伪分布式安装. 所谓 “伪分布式集群” 就是在1台PC中启动多个zookeeper的实例.“完全分布式集群” 是每1台PC启动1个ZooKeeper实例. 由于我的测试环境P ...