- 3
- 0
- 约6.04千字
- 约 7页
- 2017-02-17 发布于北京
- 举报
Location服务之LocationManager
上次介绍了位置服务中的Geocoder,这次就来介绍一下LocationManager。LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题,包括查询上一个已知位置、注册和注销来自某个LocationProvider的周期性的位置更新、注册和注销接近某个坐标时对一个已定义的Intent的触发等。今天我们就一起探讨一下LocationManager的简单应用。
在进入正题之前,朋友们需要了解与LocationManager相关的两个知识点:
provider:LocationManager获取位置信息的途径,常用的有两种:GPS和NETWORK。GPS定位更精确,缺点是只能在户外使用,耗电严重,并且返回用户位置信息的速度远不能满足用户需求。NETWORK通过基站和Wi-Fi信号来获取位置信息,室内室外均可用,速度更快,耗电更少。为了获取用户位置信息,我们可以使用其中一个,也可以同时使用两个。
LocationListener:位置监听器接口,定义了常见的provider状态变化和位置的变化的方法,我们需要实现此接口,完成自己的处理逻辑,然后让LocationManager注册此监听器,完成对各种状态的监听。
既然上面讲到位置服务的核心是LocationManager,那么我们如何取得一个LocationManager呢?像其他系统服务一样,通过以下方式即可得到一个LocationManager实例:
[java]?view plaincopy
LocationManager?locMgr?=?(LocationManager)?getSystemService(Context.LOCATION_SERVICE);??
对象实例是获取到了,可是怎么应用呢?下面就通过一个示例具体演示一下。
我们新建一个location项目。因为示例是基于地图服务的,所以创建时别忘了Build Target要选中Google APIs这一项。
然后修改/res/layout/main.xml,代码如下:
[xhtml]?view plaincopy
?xml?version=1.0?encoding=utf-8???
FrameLayout?xmlns:android=/apk/res/android??
????android:orientation=vertical??
????android:layout_width=fill_parent??
????android:layout_height=fill_parent??
????com.google.android.maps.MapView??
????????android:id=@+id/mapView??
????????android:layout_width=fill_parent??
????????android:layout_height=fill_parent??
????????android:clickable=true??
????????android:apiKey=your?apiKey?goes?here/??
????Button??
????????android:id=@+id/removeUpdates??
????????android:layout_width=fill_parent??
????????android:layout_height=wrap_content??
????????android:text=removeUpdates/??
/FrameLayout??
然后我们来看以下MainActivity.java文件,代码如下:
[java]?view plaincopy
package?com.scott.location;??
??
import?android.content.Context;??
import?android.location.Location;??
import?android.location.LocationListener;??
import?android.location.LocationManager;??
import?android.os.Bundle;??
import?android.view.View;??
import?android.widget.Button;??
import?android.widget.ImageView;??
import?android.widget.Toast;??
??
import?com.google.android.maps.GeoPoint;??
import?com.google.androi
您可能关注的文档
最近下载
- RB308A-rev1.7-晶准-恒佳盛一级代理.pdf VIP
- 小学奥数 较复杂的乘法原理.pdf VIP
- 2024年陕西省中考数学真题(学生版+解析版).docx
- 小学奥数 加乘法原理.docx VIP
- 2025烟台南山学院单招《数学》复习提分资料(含答案详解).docx VIP
- 湖南省湘一名校2026届高三上学期12月质量检测物理试卷(含答案).pdf VIP
- 2025年上海高考数学二轮复习:热点题型4 统计与概率(六大题型)原卷版+解析.pdf VIP
- WordExcel2010中文版办公专家从入门到精通.pdf VIP
- 新教材人教A版高中数学选择性必修第三册习题答案.docx VIP
- 专题08 古诗鉴赏(解析版)备战2024年中考语文真题题源解密(全国通用.docx VIP
原创力文档

文档评论(0)