android gps 获取方位_android 通过GPS获取用户地理位置并监听位置变化

本文介绍了如何在Android中使用LocationManager获取GPS地理位置,并监听位置变化。重点讲解了LocationManager的重要方法getLastKnownLocation,该方法返回设备最后一次已知的位置,但可能存在过期情况。同时提到了权限在manifest.xml中的添加方式。
摘要由CSDN通过智能技术生成

也可通过单击manifest.xml文件的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 Context.getSystemService(Context.LOCATION_SERVICE). ”

翻译一下大概意思是“LocationManager这个类提供了对系统位置服务的访问,这些服务允许应用程序获取设备地理位置的定期的更新,也可以在设备接近一个指定的地理位置的时候发起一个指定activity的intent。你不需要创建LocationManager的实例,取而代之的是通过Context.getSystemService(Context.LOCATION_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,注意这个地址可能是已经过期的,例如使用的设备可能已经被关闭或者转向了另一个位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值