转载地址: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. Windows最强ssh客户端推荐 —— Bitvise SSH Client

    原名Tunnelier,解除它是因为为了sshFQ,没想到它既有SSH Terminal,又集成SFTP,还能FQ,功能一应区全. 还支持pem证书,回想最初使用putty,还要把pem证书进行转换才 ...

  2. 怎样将游戏从Unity导到iOS设备上

    当我开始开发自己的iOS游戏时,我会考虑的第一件事便是如何将其导出到设备中,如此有效地测试我的游戏.最初,该过程看似很长且复杂,我所遇到的主要问题是,尽管存在许多资源,但是它们并非完全来自同样的地方, ...

  3. H5 Canvas | 基本操作

    <canvas> - 定义使用 JavaScript 的图像绘制. p.s. ×150. getContext是DOM对象的方法,也就是原生js方法,不能用jQuery对象直接获取 ——— ...

  4. JS遍历数组类型元素

    已停供用户不能再次停供,之前没太处理过多维数组的遍历,趁这个机会回顾一下js数组遍历 可以看出rows 获取了两条数据,为二维数组类型 方法 function batchTgWin() { var r ...

  5. PHP-解码unicode编码的中文字符

    在Python中使用 "\uxxxx".decode("unicode_escape") 1. class Helper_Tool { public stati ...

  6. PHP-客户端的IP地址伪造、CDN、反向代理、获取的那些事儿

    外界流传的JAVA/PHP服务器端获取客户端IP都是这么取的: 伪代码: 1)ip = request.getHeader("X-FORWARDED-FOR")     可伪造,参 ...

  7. php json_decode失败,返回null

    在使用json_decode之前,一定得保证字符串是utf-8编码,而执行json_decode失败的原因有很多,罗列如下: 1)编码不对: 2)字符串格式不对: 3)字符串格式对,但是有异常字符: ...

  8. docker运行环境安装-centos(一)

    在这里我们使用的是docker的社区版Docker CE,针对的是未安装docker的新的主机,如果安装过docker的早期版本,先卸载它们及关联的依赖资源,安装的版本为docker 18.03. 1 ...

  9. c#中abstract、override、new、virtual、sealed使用

    abstract     修饰类名为抽象类,修饰方法为抽象方法.如果一个类为抽象类,则这个类智能是其他某个类的基类.抽象方法在抽象类中没有函数体.抽象类中的抽象方法是没有方法体的,继承其的子类必须实现 ...

  10. iPhone6 和 iPhone 6 plus的适配

    苹果每出一款产品,都会引起广大IOS程序猿们的深深关注!是不是又该做适配了?是不是又该学习新东西了?种种的操心挂在心头. 以下我谈谈我对iPhone6 和 iPhone 6 plus适配问题的理解: ...