Android解决GPS定位时LocationManager只能绑定一个LocationListener的问题

来源:互联网 发布:魅力女生知乎 编辑:程序博客网 时间:2024/06/06 04:39

之前做项目时遇到了如题的小困难, 想了一个有点解决方法, 上周在济南发现有些初学者似乎在这里有点困扰, 就发出来供大家参考下.

首先自定义一个接口

public interface CRLocationListeneri {public void onLocationChanged(Location location);}

然后些一个类, 该类需要implements LocationListener从而代替android.location.LocationListener

public class CRLocationlistener implements LocationListener {private static final String TAG = "CRLocationlistener";private static ArrayList<CRLocationListeneri> LocationListeneriList = new ArrayList<CRLocationListeneri>(2);public void onLocationChanged(Location location) {// TODO Auto-generated method stubfor (i = 0; i < LocationListeneriList.size(); i++) {LocationListeneriList.get(i).onLocationChanged(location);}}public void onProviderDisabled(String provider) {// TODO Auto-generated method stub}public synchronized static void requestUpdates(CRLocationListeneri LocationListeneri) {LocationListeneriList.add(LocationListeneri);}public synchronized static void removeUpdates(CRLocationListeneri LocationListeneri) {LocationListeneriList.remove(LocationListeneri);}public void onProviderEnabled(String provider) {// TODO Auto-generated method stub}public void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}}
之后就可以通过

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mLocationListener);CRMyLocationOverlay.enableMyLocation();

将其与多个Listener绑定, 分别对Location进行不同的处理了.

原创粉丝点击