(转)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
Intentwhen 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_LOCATIONorACCESS_FINE_LOCATIONpermissions. 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
publicLocation 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);}returnlocation;}为什么要获取两次,因为刚才所说,一般获取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
privateLocation getLocation(){for(inti =0; i < locProvider.length, i ++){Location location = locationManager.getLocation(locProvider.get(i));if(location!=null)returnlocation;}returnnull;}另外,也可以对现有的位置提供者进行修改(系统原有的三个修改不了),不过需要
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 ...
随机推荐
- 2015 HDU 多校联赛 5317 RGCDQ 筛法求解
2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目 http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...
- C++14系列(2):C/C++的时间函数
C++笔记開始 为了好好研究下C++14.顺便复习下曾经的C++知识.搞了个git(不断完好中): https://github.com/rododo/cpp14examples.git 里面会慢慢封 ...
- git使用GitHub远程仓库
1.为什么要使用远程仓库我们知道本地仓库可以很容易地实现版本控制,但在多人开发的情况下,我们需要使用一个服务器作为远程仓库.GitHub是一个开源代码库平台以及版本控制系统. 2.GitHub仓库创建 ...
- 李洪强漫谈iOS开发[C语言-053]-小结
/* 已经学习过的C语言语法 01 基本C程序结构 int main (void) int main(int argc,char *arv){...} 02 基本语句(输入输出) 基本头文件: #in ...
- Windows 10 开发人员预览版中的新增功能(转自 IT之家)
Windows 10 开发人员预览版中的新增功能 在Win10预览版中安装工具与SDK后,即可着手创建Windows通用应用或先浏览目前的环境与此前相比都发生了什么变化. 应用建模 文件资源管理器: ...
- request的生存期只限于服务器跳转
症状: 刚才想做一个实验,在a.jsp中向request添加属性(页面编码为UTF-8),在b.jsp中删除该属性(页面编码为ISO-8859-1),通过ServletRequestAttribute ...
- Linux 命令之权限修改
chmod 改变一个文件的权限:chmod [mode] file.txt改变一个目录的权限:chmod [mode] dir改变一个目录和其子目录的权限: chmod [mode] dir - ...
- 如意云路由刷PandoraBox
目录 1 准备固件 2 使用uboot刷机 2.1 修改IP 2.2 开始刷写 3 ssh登录 4 刷回如意云 准备固件 http://downloads.openwrt.org.cn/Pandora ...
- C++ 类的对象管理模型初讲
//类的对象管理模型初讲 #include<iostream> using namespace std; class PointA{ private: int x;//占据4个字节大小的内 ...
- C++ 构造中调用构造
//构造中调用构造 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Point{ ...
我要投稿