admin管理员组

文章数量:1535477

2024年3月18日发(作者:)

【Android笔记二】Location获取地理位置信息(上)

2011 7 22暑假实训的第五天,跟大家分享以下我对Android location的学习吧,这是一个最基本的获取地

理位置信息的入门,下一次给大家介绍更质能化的地理位置选择Criteria,今天就先写简单的吧,挑选了

SDK两个重要的方法,我自己翻译了一下,也加上了自己的理解,希望大家指教,开始吧!获取跟踪你的

位置~~~

提供重要的地理位置信息服务

1 Location Manager 管理服务

2 Location Provider 提供数据的content provider

方式一:GPS 特点:精度高,耗电量大,不耗费流量权限

android:name="_FINE_LOCATION"/>

方式二:NETWORK 特点:精度低,省电,需要网络访问 权限

android:name="_FINE_LOCATION"/>

或者权限

android:name="_COARSE_LOCATION"/>

方式三PASSIVE_PROVIDER 资料比较少,只用于特定的情景下,SDK的解释是,并不自己实例化去获

取地理位置,而是通过getProvider获取其他的服务或者activity更新位置,被动的获取更新。

操作基本步骤:

1 在文件中设置权限

2 获取LocaionManager

3 选择provider

4 创建listener

/*********************************************************************************************************************

*/

第一部分权限

android 的安全服务机制,如果应用要访问本地的资源例如联系人列表、拨号、GPS或者其他应用程序的

数据,需要许可。所以要使用地理位置信息的服务需要在标签下添加

android:name="_FINE_LOCATION"/>获得许可

也可通过单击文件的permission标签可视化的添加许可

/*********************************************************************************************************************

*/

第二部分 认识LocationManager

官方SDK解释“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

temService(ON_SERVICE). ”

翻译一下大概意思是“LocationManager这个类提供了对系统位置服务的访问,这些服务允许应用程序获取

设备地理位置的定期的更新,也可以在设备接近一个指定的地理位置的时候发起一个指定activity的intent。

你不需要创建LocationManager的实例,取而代之的是通过

temService(ON_SERVICE)获取。”

重要的方法:来自官方SDK(附上我的翻译注释)

------------------------------------------------------------------------------------------------------------------------------------------

-----

public Location getLastKnownLocation (String provider)

Since: API Level 1

Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if

the device was turned off and moved to another location.

//返回一个Location,这个location标明从给定provider获得最后已知的位置,也就是最近获取的位置。

这个操作可以不必要启动provider,注意这个地址可能是已经过期的,例如使用的设备可能已经被关闭或

者转向了另一个位置。

If the provider is currently disabled, null is returned.

//如果当前的provider被禁用,函数返回null

Parameters

provider the name of the provider

Returns

the last known location for the provider, or null

Throws

SecurityException if no suitable permission is present for the provider. //前面提到的权限的问题,没

有允许会抛出安全性的异常,android系统的机制

IllegalArgumentException if provider is null or doesn't exist //非法参数异常,表示provider为

null,或者不存在

------------------------------------------------------------------------------------------------------------------------------------------

------

一些补充:

认识Location类

官方SDK解释:“A class representing a geographic location sensed at a particular time (a "fix"). A location

consists of a latitude and longitude, a UTC timestamp. and optionally information on altitude, speed, and

bearing.

大致的意思是:“这个类用来表示在一个特定时间被感应的地理位置信息(我们叫一个fix//感觉有点聚焦的

本文标签: 地理位置服务获取权限信息