前段时间在做有关定位的功能,在网上搜寻了好多个解决方案,然而并没有完全想要的那种,于是自己灵机一动,终于想到了一个不用那么费劲的解决方案了。
废话不多说,需求:项目中多个页面需要用到定位功能。
解决方案:1、自定义一个定位服务,在服务里面将位置信息放入广播信息中发送
2、将该服务在清单文件注册
3、在MyApplication中启动该服务
4、在需要用到定位功能的页面注册广播接收器接收广播(如果需要获取实时位置,就在接收到位置信息时就更新位置信息;多久刷新一次可以根据自己需要喽)
服务代码如下:
package com.dw.service; import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.GpsSatellite; import android.location.GpsStatus; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.support.annotation.Nullable; import java.util.Iterator; /** * Created by Administrator on 2017/2/14. */ public class AirnGPSService extends Service implements LocationListener, GpsStatus.Listener { private LocationManager locationManager = null; @Override public void onCreate() { super.onCreate(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); //高精度 criteria.setAltitudeRequired(false); //不要求海拔 criteria.setBearingRequired(false); //不要求方位 criteria.setCostAllowed(false); //不允许有话费 criteria.setPowerRequirement(Criteria.POWER_LOW); //低功耗 Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); locationManager.addGpsStatusListener(this); sendGPSLocationMessage(location); } else { sendGPSErrMessage("0"); } } @Override public void onDestroy() { super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onLocationChanged(Location location) { sendGPSLocationMessage(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onGpsStatusChanged(int event) { /** * GPS状态监听 */ switch (event) { // 第一次定位 case GpsStatus.GPS_EVENT_FIRST_FIX: break; // 卫星状态改变 case GpsStatus.GPS_EVENT_SATELLITE_STATUS: // 获取当前状态 if (locationManager != null) { GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 获取卫星颗数的默认最大值 int maxSatellites = gpsStatus.getMaxSatellites(); // 创建一个迭代器保存所有卫星 Iterator<GpsSatellite> iters = gpsStatus.getSatellites() .iterator(); int count = 0; String str_count = ""; while (iters.hasNext() && count <= maxSatellites) { GpsSatellite s = iters.next(); if (s.getSnr() != 0) {//只有信躁比不为0的时候才算搜到了星 count++; } } if (count > 0) { str_count = String.valueOf(count); } else { str_count = "未搜到卫星"; } sendGpsSatelliteMessage(str_count); } break; // 定位启动 case GpsStatus.GPS_EVENT_STARTED: break; // 定位结束 case GpsStatus.GPS_EVENT_STOPPED: break; } } /** * 发送位置信息 * * @param location */ private void sendGPSLocationMessage(Location location) { if (location != null) { Intent intent = new Intent("AirnGPSServiceLoc"); intent.putExtra("lat", String.valueOf(location.getLatitude())); intent.putExtra("lng", String.valueOf(location.getLongitude())); sendBroadcast(intent); } } /** * 发送卫星数量信息 */ private void sendGpsSatelliteMessage(String wx) { Intent intent = new Intent("AirnGPSServiceLocErr"); intent.putExtra("GpsSatellite", wx); sendBroadcast(intent); } /** * 发送错误信息 */ private void sendGPSErrMessage(String errMsg) { Intent intent = new Intent("AirnGPSServiceLocErr"); intent.putExtra("Errmsg", errMsg); sendBroadcast(intent); } }页面的广播接收器注册、服务清单文件注册、服务启动在此都不再多说。