最近在做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的wifi信息
  2. Android之permission权限列表2-另一篇文章,多说了一点
  3. 一篇文章带你了解 Android 消息机制的原理!
  4. Android帮助文档翻译——开发指南(十五)获取用户位置

随机推荐

  1. Android:默认手机存储路径为TF卡
  2. Android(安卓)饺子播放器
  3. 关于android.view.WindowLeaked(窗体泄露
  4. 点击Android(安卓)SDK Manager时,SDK Mana
  5. Android学习笔记——常用的基本UI组件及
  6. Android打开其它APP的两种方式
  7. [Android]Activity生命周期+启动模式
  8. Android(安卓)Kikat下由图片Uri获取Path
  9. android studio 打包过时警告问题解决方
  10. AndroidStudio使用偷懒插件Butterknife和