(转)android mock location
- android mock location
-
现在软件市场上有很多可以改变手机地理位置的软件,更改后打开微信就可以随意定位,甚至前几年有依靠这个技术打广告为生的小型公司。
一获取地理位置的方法
获取地理位置的方法一般分为两种。
1)GPS
24颗卫星定位全球(图片来自维基百科)给出详解地址:http://zh.wikipedia.org/wiki/GPS;
但是向卫星对请求信号强度的要求比较高,很多专门的硬件设备有时都达不到,所以手机使用GPS这种方法百分之九十的可能获取的是null。
2)network
这种方法其实又分为两种情况。一种是有线一种为无线。
1,有线
因为是有线的网络位置固定,每个接入点的ip为又是固定的,网络的ip和位置就产生一一对应关系。根据接入点的ip既可得到地理位置。不过这些工作似乎是系统或者网络提供商要做的,不用android的开发者解析。
2,无线
无线使用的是基站定位。实际上也就是数学上的三点定位。例如移动公司,其基站遍布全国,如果一台设备想要连无线网,附近的三个基站会根据信号强度判断出访问者的相对距离,画出三个半径为距离的园,根据三个圆的交点得知大致地理坐标。(图片来着百度百科)
3,wifi
例如某些平板电脑,不能插入网卡,不能插入手机卡,它们访问网络就都是走的wifi,那么它的地理位置获取就是根据wifi共享设备的地理位置来大致确定的。wifi共享设备的地理位置的确定参看以上1,2;
二:获取地理位置
android 提供了
locationmanager 类来管理地理位置信息,其中提供了添加,删除,筛选等很方便的功能。
public class
LocationManager
extends Object
java.lang.Object
?
android.location.LocationManager这个对象不能new ,只能从系统获取
Class Overview
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified
Intent
when the device enters the proximity of a given geographical location.You do not instantiate this class directly; instead, retrieve it through
Context.getSystemService(Context.LOCATION_SERVICE)
.Unless noted, all Location API methods require the
ACCESS_COARSE_LOCATION
orACCESS_FINE_LOCATION
permissions. If your application only has the coarse permission then it will not have access to the GPS or passive location providers. Other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.另外,这个对象获取所有的位置提供者,并且进行删除,添加,等操作。
太多了,不复制,直接给出api链接好了:http://developer.android.com/reference/android/location/LocationManager.html
在了解这个类之后再获取当前的地理位置就简单多了。网上有很多代码。
大致是获取位置提供者,循环像每个提供者索要地理信息,信息被系统包装成了Location这个类。
获取Location对象后可以调用
1
2
3
4
5
6
7
8
9
10
11
public
Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if
(location ==
null
) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return
location;
}
为什么要获取两次,因为刚才所说,一般获取GPS获取不到,不过其实获取两次应该也是null。
对于getLastKownLocation()
Location
getLastKnownLocation(String provider) Returns a Location indicating the data from the last known location fix obtained from the given provider.他接受一个提供者名字的参数,一般如果调用系统的话有:
GPS_PROVIDER, NETWORK_PROVIDER, 这两个容易理解,一个是网络一个是gps 另外,还有一个PASSIVE_PROVIDER
public static final String PASSIVE_PROVIDER
Added in API level 8
A special location provider for receiving locations without actually initiating a location fix.
This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. You can query the
getProvider()
method to determine the origin of the location update. Requires the permissionACCESS_FINE_LOCATION
, although if the GPS is not enabled this provider might only return coarse fixes.Constant Value: passive 这个究竟是什么暂时还没搞懂。
不过你也可以手动获取所有提供者,因为有可能本机的设备已经添加了某些位置提供者(下一部分分析添加位置给系统,即 虚拟地理位置),我想大多数软件都是这么写的或者说应该这么写,获取所有位置提供者,然后循环索要location:
List
getProviders(boolean enabledOnly) Returns a list of the names of location providers.List
getProviders(Criteria criteria, boolean enabledOnly) Returns a list of the names of LocationProviders that satisfy the given criteria, or null if none do.用到这两个方法,对于第二个重载的方法的第一个参数,意思是限制条件类,即根据所提供的对位置提供者的限制返回符合要求的名称。
获取String数组后就可以循环获取位置:
1
2
3
4
5
6
7
8
9
10
11
private
Location getLocation()
{
for
(
int
i =
0
; i < locProvider.length, i ++)
{
Location location = locationManager.getLocation(locProvider.get(i));
if
(location!=
null
)
return
location;
}
return
null
;
}
另外,也可以对现有的位置提供者进行修改(系统原有的三个修改不了),不过需要
1,获取LocationProvider 类
LocationProvider
getProvider(String name) Returns the information associated with the location provider of the given name, or null if no provider exists by that name.2,修改
3删除原有的
void
removeTestProvider(String provider) Removes the mock location provider with the given name.4添加修改后的。
void
addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite, boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude, boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) Creates a mock location provider and adds it to the set of active providers.当然要加入相应的权限:
三 虚拟地理位置
其实google play 上就有现成的程序:https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=zh_TW,
而且github上有托管的源代码.
具体方法是添加位置提供者,不断向位置提供者传递locaiton信息。
public void setTestProviderEnabled (String provider, boolean enabled)
Added in API level 3
Sets a mock enabled value for the given provider. This value will be used in place of any actual value from the provider.
Parameters
provider
the provider nameenabled
the mock enabled valueThrows
SecurityException
if the ACCESS_MOCK_LOCATION permission is not present or theSettings.Secure.ALLOW_MOCK_LOCATION
} system setting is not enabledIllegalArgumentException
if no provider with the given name exists这个也是需要加入特殊权限的:
另外,经过测试发现,不如直接给“gps”传位置,其他软件更容易接受,不过需要使用者把虚拟地理位置选项打开。
(转)android mock location的更多相关文章
- android mock location
原理:用 setTestProviderLocation 设置模拟gps的位置 http://androidcookbook.com/Recipe.seam?recipeId=1229 http:// ...
- android 关于Location of the Android SDK has not been setup in the preferences的解决方法
今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...
- 权限 mock location
1.集成环信的时候,该权限报错: mock location权限是是“允许程序创建模拟位置”,主要是提供用于测试.打包的时候并不需要,所以解决办法分两步: 1.声明tools 2.添加忽略:
- Android SDK location should not contain whitespace, as this cause problems with NDK tools
解决方案一: The easiest solution is to move the SDK somewhere else, where there is no space or other whit ...
- Android webview “location.replace” 不起作用
js解决方法: function locationReplace(url){ if(history.replaceState){ history.replaceState(null, document ...
- android之location 根据接口获取经纬度信息
http://maps.googleapis.com/maps/api/geocode/json?address=%E7%A6%8F%E5%BB%BA&sensor=falsehttp://m ...
- <Android> Location Service 分析
由于各种原因,老师希望我学习Android系统源码以应对可能参与的项目.我只好深入曹营,刺探军情了. 定位服务是手机上最常用的功能之一,据说也是相对比较简单的服务,所以从这里入手.其他系统服务的架构都 ...
- Android使用代码开关Location服务
Android系统中,只有系统设置里面有入口开关位置服务.其他的应用应该怎么去开关这个服务呢? 首先,应用需要有系统权限(签名),在这基础上,我们就可以通过一些手段来实现这个功能. 这里要注意一点,不 ...
- Android 权限列表
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,允许读写check-in数据库属性表的权限 ( Allows read/write acces ...
随机推荐
- iframe中子父窗口互调的js方法
转载自:http://www.cnblogs.com/chinafine/archive/2011/09/15/2177746.html 一.父窗口调用iframe子窗口方法 1.HTML语法:< ...
- alsamixer + alsactl 控制放音通道
1 使用alsamixer的gui界面配置放音(控制OUT1,OUT2的音量); 2 退出alsamixer,使用alsactl store生成配置文件,文件位于/etc/asound.state; ...
- java定时调度器解决方案分类及特性介绍
什么是定时调度器? 我们知道程序的运行要么是由事件触发的,而这种事件的触发源头往往是用户通过ui交互操作层层传递过来的:但是我们知道还有另外一种由机器系统时间触发的程序运行场景.大家想想是否遇到或者听 ...
- 工作中Hadoop,Spark,Phoenix,Impala 集群中遇到坑及解决方案
1.HDFS 修复 问题描述:其他部门在yarn平台上跑spark 程序错误的生成了海量的不到100K的小文件,导致namenode压力过大,其中一个namenode宕机后,没有及时发现 使得edit ...
- C++ 函数的扩展③--函数重载
//函数扩展--函数重载(C语言不支持函数重载) #include<iostream> using namespace std; //函数重载在本质上是相互独立的不同函数(静态链编),在c ...
- UML概述
UML (Unified Modeling Language)统一建模语言,是描述.构造和文档化系统制品的可视化语言,是一种图形表示法. UML用途:UML是一种工具,主要用在我们对软件用面向对象的方 ...
- php -- instanceof、class_exists、insterface_exists、method_exists、get_class、get_parent_class
class_exists:类是否存在 在创建对象之前判断类是否存在,如果不存在就应该先加载类,再创建对象,容错. interface_exists:接口是否存在 method_exists:方法是否存 ...
- MATLAB中TXT数据文件读取并写入元胞数组的方法与步骤
一. TXT数据文件读取 Data = load('train.txt'); %简单的文件读取,这时在工作区可以看到导入的大数据变量Data 二.大数据变量Data装入元胞数组中 D = cell ...
- VB.NET多线程入门
近期项目中遇到了一个处理速度慢阻塞用户界面操作的问题,因此想用多线程来解决. 在处理数据的循环中,新建线程,在新建的线程中处理数据.多线程同一时候处理数据,以此来达到加速的目的,使用户界面操作变得流畅 ...
- 怎么绘制旋转Chem3D模型
化学领域的专业人士常常需要绘制各种化学图形,特别是3D的图形,这个就需要用到一些化学绘图软件.Chem3D是ChemOffice的核心组件之一,可以绘制化学三维模型,包括新建.删除.旋转.移动等基础编 ...