android mock location
2014-01-26     我来说两句   来源:党玉涛  
收藏 我要投稿

现在软件市场上有很多可以改变手机地理位置的软件,更改后打开微信就可以随意定位,甚至前几年有依靠这个技术打广告为生的小型公司。

一获取地理位置的方法

获取地理位置的方法一般分为两种。

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_LOCATION or ACCESS_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 permission ACCESS_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 name

enabled
the mock enabled value

Throws

SecurityException
if the ACCESS_MOCK_LOCATION permission is not present or the Settings.Secure.ALLOW_MOCK_LOCATION} system setting is not enabled

IllegalArgumentException
if no provider with the given name exists

这个也是需要加入特殊权限的:

另外,经过测试发现,不如直接给“gps”传位置,其他软件更容易接受,不过需要使用者把虚拟地理位置选项打开。

(转)android mock location的更多相关文章

  1. android mock location

    原理:用 setTestProviderLocation 设置模拟gps的位置 http://androidcookbook.com/Recipe.seam?recipeId=1229 http:// ...

  2. 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 ...

  3. 权限 mock location

    1.集成环信的时候,该权限报错: mock location权限是是“允许程序创建模拟位置”,主要是提供用于测试.打包的时候并不需要,所以解决办法分两步: 1.声明tools 2.添加忽略:

  4. 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 ...

  5. Android webview “location.replace” 不起作用

    js解决方法: function locationReplace(url){ if(history.replaceState){ history.replaceState(null, document ...

  6. android之location 根据接口获取经纬度信息

    http://maps.googleapis.com/maps/api/geocode/json?address=%E7%A6%8F%E5%BB%BA&sensor=falsehttp://m ...

  7. <Android> Location Service 分析

    由于各种原因,老师希望我学习Android系统源码以应对可能参与的项目.我只好深入曹营,刺探军情了. 定位服务是手机上最常用的功能之一,据说也是相对比较简单的服务,所以从这里入手.其他系统服务的架构都 ...

  8. Android使用代码开关Location服务

    Android系统中,只有系统设置里面有入口开关位置服务.其他的应用应该怎么去开关这个服务呢? 首先,应用需要有系统权限(签名),在这基础上,我们就可以通过一些手段来实现这个功能. 这里要注意一点,不 ...

  9. Android 权限列表

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,允许读写check-in数据库属性表的权限 ( Allows read/write acces ...

随机推荐

  1. 使用digitalocean搭建v·p·s

    这几天digitalocean开始猛卡,一顿操作之后连不上了=_=遂复习了一下怎么搭vps 准备工作 事先准备好Putty,直接百度搜索下载即可(也可直接使用digitalocean的access c ...

  2. 1、改变 vs编辑器的主题

    打开 visual studio, 在菜单栏选择  工具 -> 扩展和更新 -> 输入 “color theme” 安装完成后,选择样式. 选择好样式后, vs 立即改变主题.再次打开样式 ...

  3. jquery的liveQuery插件

    一.livequery插件简介 jQuery的事件绑定功能使得jQuery代码与HTML代码能够完全分离,这样代码的层次关系更加清晰,维护起来也更加简单.然而对于动态加载到页面的HTML元素,每次都需 ...

  4. Ubuntu/Debian交叉编译安装ARM平台版本的ffmpeg

    1 准备工作 (1)libmp3lame库 下载:    wget http://downloads.sourceforge.net/lame/lame-3.99.tar.gz 解压 编译:./con ...

  5. 延时NSTimer

    import Foundationimport UIKit class YijfkController:UIViewController{ override func viewDidLoad() { ...

  6. API - 使用Default对象 - 基础篇

    在编写Spider Studio脚本时, Default对象是最常用最重要的一个, 其类型定义如下: Webus3.Spider.Controls.JQueryBrowser Default; 下面介 ...

  7. KMP算法完整教程 (下)

    下面我们用数学归纳法来解决这个填值的问题. 这里我们借鉴数学归纳法的三个步骤(或者说是动态规划?): 1.初始状态 2.假设第j位以及第j位之前的我们都填完了 3.推论第j+1位该怎么填 初始状态我们 ...

  8. 关于ubuntu系统boot分区空间不足而又无法卸载旧内核的解决方法

    2016年09月03日 14:16:45 萧氏一郎 阅读数:7802 标签: ubuntuboot分区清理更多 个人分类: linux   版权声明:本文为本猿原创文章,转载务必注明出处,多谢. ht ...

  9. wrk -- 小巧轻盈的 http 性能测试工具.

    标签: wrk http 性能 | 发表时间:2015-06-21 00:55 | 作者:zjumty 出处:http://www.iteye.com 测试先行是软件系统质量保证的有效手段. 在单元测 ...

  10. 树形dp - BNU 39572 Usoperanto

    Usoperanto Problem's Link Mean: 给定n个单词,每个单词可以作为形容词来修饰其他单词. 如果当前单词Wi修饰Wj,那么这个修饰的代价是:Wi~Wj之间的单词的总长度. 你 ...