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:

  1. package com.home.locationprovider;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.location.Criteria;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;
  12. public class LocationProviderTestActivity extends Activity {
  13. private ListView listView;
  14. private LocationManager locationManager;
  15. // 存放LocationProvider名称的集合
  16. private List<String> providerNames = new ArrayList<String>();
  17. private ArrayAdapter<String> adapter;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. listView = (ListView) findViewById(R.id.main_lv_show);
  23. // 获取系统的LocationManager对象
  24. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  25. }
  26. public void click(View v) {
  27. if (v.getId() == R.id.main_btn_get_all) {
  28. // 获取系统所有的LocationProvider的名称
  29. providerNames = locationManager.getAllProviders();
  30. adapter = new ArrayAdapter<String>(this,
  31. android.R.layout.simple_list_item_1, providerNames);
  32. listView.setAdapter(adapter);
  33. }
  34. if (v.getId() == R.id.main_btn_get_criteria) {
  35. // 创建一个LocationProvider的过滤条件
  36. Criteria criteria = new Criteria();
  37. // 设置要求LocationProvider必须使免费的
  38. criteria.setCostAllowed(false);
  39. // 设置要求LocationProvider能提供高度信息
  40. criteria.setAltitudeRequired(true);
  41. // 设置要求LocationProvider能提供方向信息
  42. criteria.setBearingRequired(true);
  43. // 获取系统所有符合条件的LocationProvider的名称
  44. providerNames = locationManager.getProviders(criteria, true);
  45. adapter = new ArrayAdapter<String>(this,
  46. android.R.layout.simple_list_item_1, providerNames);
  47. listView.setAdapter(adapter);
  48. }
  49. if (v.getId() == R.id.main_btn_get_byname) {
  50. providerNames.clear();
  51. // 根据名称获取指定的LocationProvider的名称
  52. providerNames.add(locationManager.getProvider(
  53. LocationManager.GPS_PROVIDER).getName());
  54. adapter = new ArrayAdapter<String>(this,
  55. android.R.layout.simple_list_item_1, providerNames);
  56. listView.setAdapter(adapter);
  57. }
  58. }
  59. }

布局XML:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <Button
  6. android:id="@+id/main_btn_get_all"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:onClick="click"
  10. android:text="获取所有LocationProvider" />
  11. <Button
  12. android:id="@+id/main_btn_get_criteria"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:onClick="click"
  16. android:text="根据条件获取LocationProvider" />
  17. <Button
  18. android:id="@+id/main_btn_get_byname"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:onClick="click"
  22. android:text="获取指定的LocationProvider" />
  23. <ListView
  24. android:id="@+id/main_lv_show"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content" />
  27. </LinearLayout>

权限:

  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

附上图片效果:

转自这里:http://blog.csdn.net/u010142437/article/category/1435920

Android中GPS类及方法简介的更多相关文章

  1. 【转】Android中JNI的使用方法

    Android中JNI的使用方法 首先看一下Android平台的框架图:(网上盗用) 可以看到Android上层的Application和ApplicationFramework都是使用Java编写, ...

  2. Python Python-MySQLdb中的DictCursor使用方法简介

    Python-MySQLdb中的DictCursor使用方法简介 by:授客 QQ:1033553122     DictCursor的这个功能是继承于CursorDictRowsMixIn,这个Mi ...

  3. Android中Cursor类的概念和用法[转]

    首页 > 程序开发 > 移动开发 > Android > 正文   Android中Cursor类的概念和用法 2011-09-07      0个评论       收藏    ...

  4. Android中Application类的详解:

    Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...

  5. Android中JNI的使用方法(转载)

    Android中JNI的使用方法 首先看一下Android平台的框架图:(网上盗用) 可以看到Android上层的Application和ApplicationFramework都是使用Java编写, ...

  6. Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法

    Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...

  7. android中的提示信息显示方法(toast应用)

    android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...

  8. android中Handle类的用法

    android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...

  9. Android中一个类实现的接口数不能超过七个

    近期一段时间,在开发Android应用程序的过程中,发现Android中一个类实现的接口数超过七个的时候,常常会出现超过第7个之后的接口不能正常使用.

随机推荐

  1. WebForm和WinForm取当前根目录的通用的方法[转载]

    转自:WebForm和WinForm取当前根目录的通用的方法 某些环境下用 System.Web.HttpContext.Current.Server.MapPath 取不到目录. 可以用下面两个方法 ...

  2. c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 .

    // c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 分类: c# 2013-02-06 15:18 3008人阅读 评论(0) 收藏 举报 可以实现 ...

  3. MSSERVER创建链接服务器

    exec sp_addlinkedserver 'DB_RASS','','SQLOLEDB','127.0.0.1' ' exec sp_serveroption 'DB_RASS','rpc ou ...

  4. java读取属性文件propertie中文乱码问题

    在属性文件中使用Unicode编码中文 propertie文件默认编辑就是Unicode编码

  5. android text

    "@you bang--- go on -------" 需要做分享内容,前面有段格式固定写死,同时颜色为灰色:后面的内容可以编辑,颜色为黑色,同时支持多行 有人用textview ...

  6. 抓发请求&设置默认工程

    反向代理:外网服务器接受来自Internet的请求,转发到内网服务器. iptables转发:只有root才能使用1024以下的端口,域名的默认端口为80,一般不会给开发者提供root密码的.一台优质 ...

  7. Android Studio签名打包的两种方式

    签名打包的两种方式: 注:给我们自己开发的app签名,就代表着我自己的版权,以后要进行升级,也必须要使用相同的签名才行.签名就代表着自己的身份(即keystore),多个app可以使用同一个签名. 如 ...

  8. 斯坦福第十课:应用机器学习的建议(Advice for Applying Machine Learning)

    10.1  决定下一步做什么 10.2  评估一个假设 10.3  模型选择和交叉验证集 10.4  诊断偏差和方差 10.5  归一化和偏差/方差 10.6  学习曲线 10.7  决定下一步做什么 ...

  9. font-face跨域办法

    font-face是现在比较流行的技术,可以矢量化你的图标,更改颜色方便等等.如果你想更进一步了解他,请点击这里(CSS3 icon font完全指南)今晚有网友问到font-face跨域在nginx ...

  10. 第12周&第13周

    12&13:STL Standard Template Library sort, binary_search/lower_bound/upper_bound, multiset, set, ...