在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法。在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度、经度、高度、方向、移动速度等。代码如下:

Activity:

package comhome.location;import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.widget.EditText;public class LocationTestActivity extends Activity {// 定义LocationManager对象private LocationManager locationManager;private EditText show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);show = (EditText) findViewById(R.id.main_et_show);// 获取系统LocationManager服务locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);// 从GPS获取最近的定位信息Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);// 将location里的位置信息显示在EditText中updateView(location);// 设置每2秒获取一次GPS的定位信息locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 8, new LocationListener() {@Overridepublic void onLocationChanged(Location location) {// 当GPS定位信息发生改变时,更新位置updateView(location);}@Overridepublic void onProviderDisabled(String provider) {updateView(null);}@Overridepublic void onProviderEnabled(String provider) {// 当GPS LocationProvider可用时,更新位置updateView(locationManager.getLastKnownLocation(provider));}@Overridepublic void onStatusChanged(String provider, int status,Bundle extras) {}});}private void updateView(Location location) {if (location != null) {StringBuffer sb = new StringBuffer();sb.append("实时的位置信息:\n经度:");sb.append(location.getLongitude());sb.append("\n纬度:");sb.append(location.getLatitude());sb.append("\n高度:");sb.append(location.getAltitude());sb.append("\n速度:");sb.append(location.getSpeed());sb.append("\n方向:");sb.append(location.getBearing());sb.append("\n精度:");sb.append(location.getAccuracy());show.setText(sb.toString());} else {// 如果传入的Location对象为空则清空EditTextshow.setText("");}}}

布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <EditText        android:id="@+id/main_et_show"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:cursorVisible="false"        android:editable="false" /></LinearLayout>

权限:

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

附上图片效果:


如果把该程序与Google Map结合,让程序根据GPS提供的信息实时地显示用户在地图上的位置,即可开发出GPS导航系统。

更多相关文章

  1. Android获取屏幕宽度高度并动态设置控件宽度,比例等
  2. Android(安卓)集成微信sdk 实现微信登录
  3. Android权限探究——获取正在运行的应用/进程列表
  4. 【Android】获取设备型号、SDK版本及其系统版本
  5. Android在onCreate()方法中动态获取TextView控件的高度
  6. 如何获取android的相关信息,以及安装路径等信息的获取等
  7. Android(安卓)Toolbar左、中、右对齐
  8. android中使用GoogleMap的地理位置服务
  9. Android常用代码之APK root权限静默安装

随机推荐

  1. android开发实例06:popwindow实现下拉菜单
  2. RecyclerView 局部刷新、删除错乱
  3. android studio 3.0 -Aapt2Exception
  4. Android网络通信库Volley简介(Google IO
  5. android2.3修改ethernet默认为不选中状态
  6. 使用和管理Android中Activity的切换动画(
  7. Android(安卓)ListView列表视图的使用方
  8. android 建立sdcard 并放文件 在程序中取
  9. 用Classpy分析.dex文件
  10. SystemServer(一):恢复出厂设置后wifi、数