Android 从活动侦听绑定位置服务

Android 从活动侦听绑定位置服务,android,service,android-activity,Android,Service,Android Activity,我有一个位置服务,可以在我的应用程序中更新位置。我将它绑定到每个需要位置数据的活动,现在我想知道在这些活动中,服务中的位置侦听器何时接收到onLocationChanged、onProviderEnabled等事件。。。我该怎么做 在我的活动中 private ServiceConnection mConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(

我有一个位置服务,可以在我的应用程序中更新位置。我将它绑定到每个需要位置数据的活动,现在我想知道在这些活动中,服务中的位置侦听器何时接收到onLocationChanged、onProviderEnabled等事件。。。我该怎么做

在我的活动中

private ServiceConnection mConnection;     

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Bind location service
    bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE);

    // Activity stuff...
}

@Override
protected void onDestroy() {
    super.onDestroy(); 
    // Unbind LocationService
    context.unbindService(mConnection);
}
LocationService.java

public class LocationService extends Service implements LocationListener {

    LocationManager locationManager; 

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {        
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){    

            // Update after minimum 5 minutes and if user has moved at least 100 meters.
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);        

            Location loc = getBestLocation(locationManager);
            if(loc!=null){
                GlobalVars.lat = (Double) (loc.getLatitude());
                GlobalVars.lng = (Double) (loc.getLongitude());
            }
        }
    }

    public void onLocationChanged(Location loc) {        
        GlobalVars.lat = (Double) (loc.getLatitude());
        GlobalVars.lng = (Double) (loc.getLongitude()); 
    }

    public static Location getBestLocation(LocationManager locationManager) {

        Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        // If both are available, get the most recent
        if(location_gps!=null && location_network !=null) {
            return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
        }
        else if(location_gps==null && location_network ==null){
            return null;
        }
        else
            return (location_gps==null)?location_network:location_gps;

    }

    public void onProviderEnabled(String s){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 0, this); 
    }
    public void onProviderDisabled(String s){
        locationManager.removeUpdates(this);
        GlobalVars.lat = null;
        GlobalVars.lng = null;  
    }
    public void onStatusChanged(String s, int i, Bundle b){}

    @Override
    public void onDestroy() {       
        locationManager.removeUpdates(this);
    }
}

我会这样做:

  • 创建LocationListener类
  • 每次我得到一个新的位置,发送带有特定预定义动作的广播消息,并在附加中放置lat和lon
  • 每个活动都使用intentFilter(操作)为此消息创建广播侦听器
  • 从意图中获取额外信息