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:
- DDMS sets location for GPS location provider only. If your application uses network provider then you are out of luck.
- 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的更多相关文章
- android中获取root权限的方法以及原理(转)
一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...
- Android 中运行时权限获取联系人信息 Demo
代码比较简单... AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <m ...
- Android中的文件权限操作
默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...
- android 中使用自定义权限在广播中的利用
1.在一个进程中发送一个有自定义权限的广播,另外一个进程中拥有广播接受者接受到该广播 <?xml version="1.0" encoding="utf-8&quo ...
- android 中使用自定义权限
1.如果在一个进程中启动另外一个进程的activity <?xml version="1.0" encoding="utf-8"?> <man ...
- 快速解决Android中的selinux权限问题【转】
本文转载自:http://blog.csdn.net/mike8825/article/details/49428417 版权声明:本文为博主原创文章,未经博主允许不得转载. 关于selinux的详细 ...
- Android中如何做到自定义的广播只能有指定的app接收
今天没吊事,又去面试了,具体哪家公司就不说了,因为我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目,好吧,我错了...其实当我们已经在工作的时候,我们可以在空闲的时间去面 ...
- Android中的各种访问权限Permission含义
android.permission.EXPAND_STATUS_BAR 允许一个程序扩展收缩在状态栏,android开发网提示应该是一个类似Windows Mobile中的托盘程序 android. ...
- Android中的WebView进行直接加载网页(要注意解决权限问题)
我们都知道Android的网络功能很不错,当然Android中WebView组件也挺不错,可以直接进行加载网页,我们可以把这个看做一个小型的浏览器\ [注]以下的一些内容我翻译了一下文档,可能有些翻译 ...
随机推荐
- html 接收GET请求参数
function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +" ...
- win7多重网络之默认网关问题
如果有谁做硬件的在用MAC,跟我讲一声好用不好用...有常用软件清单最好.windows已经用的无力吐槽了. 机子上有两块网卡,一块有线一块无线,有线的连接内部网络,无线连接外部网络, 问题描述: 内 ...
- struts2配置默认Action
作用:当一个请求无法匹配到任何一个struts的action时,可以配置一个默认Action 例如:当请求路径不正确时,跳转到一个404.jsp页面 <package extends=" ...
- FIS3项目构建
概述 FIS3采取了类似CSS语法一样的配置风格,易于理解与上手.FIS3 是面向前端的工程构建工具.解决前端工程中性能优化.资源加载(异步.同步.按需.预加载.依赖管理.合并.内嵌).模块化开发.自 ...
- 网络请求框架---Volley
去年的Google I/O大会为android开发者带来了一个网络请求框架,它的名字叫做Volley.Volley诞生的使命就是让Android的网络请求更快,更健壮,而且它的网络通信的实现是基于Ht ...
- NFS介绍
一.NFS服务介绍 NFS是 Network File system的缩写 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机 ...
- 解决Janusgraph索引状态不变更的问题
JanusGraph的索引因为要同步不同实例及不同后端的数据,因此不是实时能够完成的,视配置,网络和数据量不同,建立/生效索引通常需要一段时间,这也是为什么创建索引时会创建wait()的原因. 在实践 ...
- PHP时间戳和日期相互转换(文字有问题)
在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明. 1.php中时间转换函数 strtotime ...
- 在Unity控制台下使用富文本
之前都不知道,最近看了csdn一位开发者的博文突然发现 <b>asd</b> <color="red">asd</color> &l ...
- atitit.LimeSurvey 安装 attilax 总结
LimeSurvey 安装 (OSSEZ) CHS 技术参考, 2011-05-02 Author: YUCHENG HU, HA Websystems, Inc. 备忘 Copyright © HA ...