Android中GPS类及方法简介
GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能。GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程序又十分简单,Android为此提供LocationManager类及其他几个辅助类,开发人员可以非常方便地开发出GPS应用。在程序中,通过getSystemService获得LocationManager对象之后,就可以调用LocationManager提供的常用方法:
在上面方法中涉及另一个重要的类:LocationProvider(定位提供者),就是GPS定位组件的抽象表示,它提供了如下方法来获取定位组件的相关信息:
另外,GPS支持还涉及到另一个类:Location,它是一个代表位置信息的抽象类,它提供如下方法来获取定位信息:
另外,在获取LocationProvider时,往往需要加上过滤条件,即Criteria,Criteria提供如下方法来设置过滤条件:
使用上面三个类及其提供的方法就可以获取GPS定位信息了,步骤如下:
1、获取系统的LocationManager对象。
2、使用LocationManager,通过指定LocationProvider来获取定位信息,定位信息由对象Location表示。
3、从Location对象中获取定位信息。
下面用一个简单的示例来演示,根据不同方式获取LocationProvider定位信息,代码如下:
Activity:
- package com.home.locationprovider;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.location.Criteria;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class LocationProviderTestActivity extends Activity {
- private ListView listView;
- private LocationManager locationManager;
- // 存放LocationProvider名称的集合
- private List<String> providerNames = new ArrayList<String>();
- private ArrayAdapter<String> adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listView = (ListView) findViewById(R.id.main_lv_show);
- // 获取系统的LocationManager对象
- locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- }
- public void click(View v) {
- if (v.getId() == R.id.main_btn_get_all) {
- // 获取系统所有的LocationProvider的名称
- providerNames = locationManager.getAllProviders();
- adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, providerNames);
- listView.setAdapter(adapter);
- }
- if (v.getId() == R.id.main_btn_get_criteria) {
- // 创建一个LocationProvider的过滤条件
- Criteria criteria = new Criteria();
- // 设置要求LocationProvider必须使免费的
- criteria.setCostAllowed(false);
- // 设置要求LocationProvider能提供高度信息
- criteria.setAltitudeRequired(true);
- // 设置要求LocationProvider能提供方向信息
- criteria.setBearingRequired(true);
- // 获取系统所有符合条件的LocationProvider的名称
- providerNames = locationManager.getProviders(criteria, true);
- adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, providerNames);
- listView.setAdapter(adapter);
- }
- if (v.getId() == R.id.main_btn_get_byname) {
- providerNames.clear();
- // 根据名称获取指定的LocationProvider的名称
- providerNames.add(locationManager.getProvider(
- LocationManager.GPS_PROVIDER).getName());
- adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, providerNames);
- listView.setAdapter(adapter);
- }
- }
- }
布局XML:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/main_btn_get_all"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="获取所有LocationProvider" />
- <Button
- android:id="@+id/main_btn_get_criteria"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="根据条件获取LocationProvider" />
- <Button
- android:id="@+id/main_btn_get_byname"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="获取指定的LocationProvider" />
- <ListView
- android:id="@+id/main_lv_show"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
权限:
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
附上图片效果:
转自这里:http://blog.csdn.net/u010142437/article/category/1435920
Android中GPS类及方法简介的更多相关文章
- 【转】Android中JNI的使用方法
Android中JNI的使用方法 首先看一下Android平台的框架图:(网上盗用) 可以看到Android上层的Application和ApplicationFramework都是使用Java编写, ...
- Python Python-MySQLdb中的DictCursor使用方法简介
Python-MySQLdb中的DictCursor使用方法简介 by:授客 QQ:1033553122 DictCursor的这个功能是继承于CursorDictRowsMixIn,这个Mi ...
- Android中Cursor类的概念和用法[转]
首页 > 程序开发 > 移动开发 > Android > 正文 Android中Cursor类的概念和用法 2011-09-07 0个评论 收藏 ...
- Android中Application类的详解:
Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...
- Android中JNI的使用方法(转载)
Android中JNI的使用方法 首先看一下Android平台的框架图:(网上盗用) 可以看到Android上层的Application和ApplicationFramework都是使用Java编写, ...
- Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...
- android中的提示信息显示方法(toast应用)
android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...
- android中Handle类的用法
android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...
- Android中一个类实现的接口数不能超过七个
近期一段时间,在开发Android应用程序的过程中,发现Android中一个类实现的接口数超过七个的时候,常常会出现超过第7个之后的接口不能正常使用.
随机推荐
- iOS10适配及Xcode8配置
一.证书管理 用Xcode8打开工程后,比较明显的就是下图了,这个是苹果的新特性,可以帮助我们自动管理证书.建议大家勾选这个Automatically manage signing(Ps.但是在bea ...
- 通过java获取html中所有的图片路径
/** * 获取网页上所有的图片路径 * @param htmlCode * @return */ public static List<String> getImageSrc(Strin ...
- git flow的安装和使用
确保安装了git 1.windows系统下安装 进入cmd clone github上的gitflow到一个文件夹下 我这里clone到 c:\gitflow git clone git://gith ...
- 在cocos2d里面如何使用Texture Packer和像素格式来优化spritesheet
免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...
- 手机数据抓包以及wireshark技巧
本文主要讨论一种非常方便的抓取Android和iphone手机网络数据包的办法,以及介绍wireshark最常用的技巧 抓包工具介绍 (1).网页抓包工具 Chrome浏览器插件 FireBug 插件 ...
- Session为null无法访问
我们在一般处理程序中需要访问Session为null 无法访问和操作 处理方案: 1.导入命名空间 System.Web.SessionState 2.实现IRequiresSessionState ...
- 写给自己看的Linux运维基础(四) - python环境
pip - Python包管理工具 https://pip.pypa.io/en/latest/installing.html wget https://bootstrap.pypa.io/get-p ...
- [游戏学习29] Win32 图像处理1
>_<:bmp格式的简单处理: >_<:变暗RGB同时除以某一值 >_<:出现轮廓的是通道相减 >_<:最后一个是颜色提取 >_ ...
- Java经典类库-Guava中的函数式编程讲解
如果我要新建一个java的项目,那么有两个类库是必备的,一个是junit,另一个是Guava.选择junit,因为我喜欢TDD,喜欢自动化测试.而是用Guava,是因为我喜欢简洁的API.Guava提 ...
- Linux安装snmp
1.yum安装 yum -y install net-snmp* 2.修改配置文件/etc/snmp/snmpd.conf com2sec notConfigUser default public 默 ...