资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
本代码实现 GPS 定位,并定时在界面上呈现定位的精度以及在用卫星数量。布局代码如下:首先写后台的 LBSServiceListener 实现 LocationListener 接口,在这个 LBSServiceListener 中可以重写方法,代码如下:package com.exams.demo10_lbs;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.TimeZone;import android.location.Location;import android.location.LocationListener;import android.os.Bundle;import android.util.Log;public class LBSServiceListener implements LocationListener public int GPSCurrentStatus;public Location currentLocation;public void onLocationChanged(Location location) / TODO Auto-generated method stub/-/ Called when a new location is found by the location provider.if (currentLocation != null) if (isBetterLocation(location, currentLocation) / Log.v(GPSTEST, Its a better location);currentLocation = location; else / Log.v(GPSTEST, Not very good!); else / Log.v(GPSTEST, Its first location);currentLocation = location;/ 将数据通过 get 的方式发送到服务器,服务器可以根据这个数据进行跟踪用户的行走状态private void doGet(String string) / TODO Auto-generated method stub/public void onProviderDisabled(String provider) / TODO Auto-generated method stub/ if (LocationManager.NETWORK_PROVIDER).equals(provider) / locationManager.removeUpdates(this);/ else if (LocationManager.GPS_PROVIDER).equals(provider) / locationManager.removeUpdates(this);/ public void onProviderEnabled(String provider) / TODO Auto-generated method stubpublic void onStatusChanged(String provider, int status, Bundle extras) / TODO Auto-generated method stub GPSCurrentStatus = status;private static final int CHECK_INTERVAL = 1000 * 30;protected boolean isBetterLocation(Location location,Location currentBestLocation) if (currentBestLocation = null) / A new location is always better than no locationreturn true;/ Check whether the new location fix is newer or olderlong timeDelta = location.getTime() - currentBestLocation.getTime();boolean isSignificantlyNewer = timeDelta CHECK_INTERVAL;boolean isSignificantlyOlder = timeDelta 0;/ If its been more than two minutes since the current location,/ use the new location/ because the user has likely movedif (isSignificantlyNewer) return true;/ If the new location is more than two minutes older, it must/ be worse else if (isSignificantlyOlder) return false;/ Check whether the new location fix is more or less accurateint accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy();boolean isLessAccurate = accuracyDelta 0;boolean isMoreAccurate = accuracyDelta 200;/ Check if the old and new location are from the same providerboolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider();/ Determine location quality using a combination of timeliness and/ accuracyif (isMoreAccurate) return true; else if (isNewer & !isLessAccurate) return true; else if (isNewer & !isSignificantlyLessAccurate& isFromSameProvider) return true;return false;/* Checks whether two providers are the same */private boolean isSameProvider(String provider1, String provider2) if (provider1 = null) return provider2 = null;return provider1.equals(provider2);这样这个 LBSServiceListener 已经实现,其中我使用 isBetterLocation()方法对 Location 做判断。下面代码是 GpsSatelliteListener 实现 GpsStatus.Listener:package com.exams.demo10_lbs;import android.location.GpsStatus;import android.location.GpsStatus.Listener;public class GpsSatelliteListener implements Listener public void onGpsStatusChanged(int event) / TODO Auto-generated method stubswitch (event) / 第一次定位case GpsStatus.GPS_EVENT_FIRST_FIX:break;/ 卫星状态改变case GpsStatus.GPS_EVENT_SATELLITE_STATUS:break;/ 定位启动case GpsStatus.GPS_EVENT_STARTED:break;/ 定位结束case GpsStatus.GPS_EVENT_STOPPED:break;重点是 LBSService 它是继承了 Service.在这个类中,重点通过 onStartCommand 方法和 ACTIVITY 沟通,实现将定位的经纬度等信息返回给 activity 的 textview 文本呈现出来。package com.exams.demo10_lbs;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Iterator;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.location.GpsSatellite;import android.location.GpsStatus;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Binder;import android.os.Bundle;import android.os.IBinder;import android.util.Log;public class LBSService extends Service public static final String TAG = LBSService;/ 30000ms -minimum time interval between location updates, in millisecondsprivate static final long minTime = 30000;/ 最小变更距离 10m -minimum distance between location updates, in metersprivate static final float minDistance = 10;String tag = this.toString();private LocationManager locationManager;private Location
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号