zoukankan      html  css  js  c++  java
  • (转)android mock location

    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”传位置,其他软件更容易接受,不过需要使用者把虚拟地理位置选项打开。

  • 相关阅读:
    <转载>asp.net中向数据库中插入数据时如何获得当前插入行的主键?
    <转载>HTML实体字符
    以当前系统默认的编码写入文件
    <转载>SQL Server 数据库 执行命令(方法 2:带参数的 SQL 命令)
    Windows7无法访问Windows2003共享的解决方案
    MVC Music Store 在线音乐商店示例分析(12)StoreManagerController
    MVC Music Store 在线音乐商店示例分析(6)ShoppingCart
    MVC Music Store 在线音乐商店示例分析(10)ShoppingCartController
    MVC Music Store 在线音乐商店示例分析(3)Album
    MVC Music Store 在线音乐商店示例分析(9)HomeController
  • 原文地址:https://www.cnblogs.com/cute/p/4142558.html
Copyright © 2011-2022 走看看