最近在做Android手机应用开发,还是很有意思的。其实如果只是做简单手机应用开发而不是手机游戏开发的话,还是很简单的。把主要的控件掌握了,就可以开发简单的应用了。

下面主要说一下在Android中使用GPS功能。

开发由于GPS功能时,常与Google Map相关,因此先推荐一篇讲解Google Map的文章:
http://mobiforge.com/developing/story/using-google-maps-android
该文章详细的讲解了Android中如何使用Google Map的各种功能。文章甚好,强烈推荐。

看完了如上文章后,我们就来讲解下如何使用GPS。

首先在AndroidManifest.xml中添加位置服务权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

然后再看如下代码例:
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (loc != null) {lat = loc.getLatitude();Log.d(TAG, "latitude: " + lat);lng = loc.getLongitude();Log.d(TAG, "longitude: " + lng);}

先注册LocationManager,然后就可以通过访问getLastKnownLocation得到当前的GPS坐标。是不是很简单。

既然是GPS,我们当然不只是想知道当前的位置,更重要的是要随着位置的移动,GPS信息也要更新。那么我们需要怎么做呢?

还先看如下代码例:
LocationListener locLis = new MyLocationListener();locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10,locLis);.........public class MyLocationListener implements LocationListener {@Overridepublic void onLocationChanged(Location loc) {if (loc != null) {p = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));mc.animateTo(p);mc.setZoom(14);mc.setCenter(p);}}@Overridepublic void onProviderDisabled(String provider) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}}

声明自己的LocationListener后,调用requestLocationUpdates方法,就可以得到最新的GPS信息。

常用方法说明:
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

当时间超过minTime(单位:毫秒),或者位置移动超过minDistance(单位:米),就会调用listener中的方法更新GPS信息。
官方文档中有如下说明:
1. minTime的值最好是不小于60000(即:1分钟),这样会更加高效且节电。
2. 如果要尽可能实时的更新GPS信息,请将minTime和minDistance都设置成0。

以说明下来自官方文档:
Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.

It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.

In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.

The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended.

The calling thread must be a Looper thread such as the main thread of the calling Activity.

更多相关文章

  1. Android(安卓)Socket 开发
  2. NDK开发
  3. Android开发如何正确使用WebView
  4. 【Android(安卓)开发】:数据存储之 SQLite 数据库操作(三)
  5. Android开发易错点总结
  6. Android(安卓)UI开发第三十九篇――Tab界面实现汇总及比较
  7. 第四章 Android开发三大基石—Activity、Service和Handler(7)
  8. [原创]Android中LocationManager的简单使用,获取当前位置
  9. 第一篇 入门必备 (Android学习笔记)

随机推荐

  1. Android: 发送虚拟按键
  2. Android--Android四大组件(一)(Android s
  3. Android设计模式系列--工厂方法模式
  4. android之ExoPlayer探索
  5. Plugin Error: required plugin “Androi
  6. 【Android】SlidingMenu属性详解
  7. Android(安卓)Studio加载字体
  8. Android与H5的交互
  9. Android 触控事件解析 - Mastering The A
  10. Android Animation动画实战(二):从屏幕底部