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组件也挺不错,可以直接进行加载网页,我们可以把这个看做一个小型的浏览器\ [注]以下的一些内容我翻译了一下文档,可能有些翻译 ...
随机推荐
- Unity 3d导入3dMax模型 产生若干问题
Unity 3d导入3dMax模型 会产生若干问题,按照官方 的说明,将max 模型导成fbx文件 导入untiy似乎也不能解决 1.x轴向偏转3dmax模型导入后自动有一个x轴270度的偏转,巧合的 ...
- iOS开发必备指南合集之游戏接入GameCenter 指南
原地址:http://bbs.9ria.com/thread-248408-1-1.html iTunes Connect 设置 首先,申请一个应用程序,不必提交.目地是为了得到Bundle ID. ...
- java Web笔记
Get:标记在地址栏中 放在请求行中显示get?Username=...Password=... Post:使用的是二进制的上传下载 请求行不显示信息 数据传输量大数据隐蔽对文本没有 pst不能再浏览 ...
- spring日志加载代码解析
项目用的是springmvc+spring+mybatis框架, 配置日志的时候非常简单,仅仅是把commons-logging.log4j,还有slf4j-log4j三个日志相关的jar包导入项目, ...
- 一个简单的python爬虫(转)
# -*- coding: utf-8 -*- #--------------------------------------- # 程序:百度贴吧爬虫 # 版本:0.1 # 作者:why # 日期: ...
- 一个漂亮而强大的自定义view
代码地址如下:http://www.demodashi.com/demo/13502.html 简介 主要提供一个漂亮而强大的自定义SeekBar,进度变化由提示牌 (sign)展示,具有强大的属性设 ...
- html调用摄像头的方法汇总
1.在PC端打开摄像头的方法:(移动端不能使用) 能够实现打开摄像头并截图 <!doctype html> <html lang="en"> <hea ...
- Linux系统编程之----》信号
"===信号========================================================================================= ...
- 測试赛C - Eqs(哈希)
C - Eqs Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- Python爬去图片实例,python 爬取图片
# coding:utf-8 import requests import re import time proxies = { "http": "http://124. ...