从活动中收听绑定的位置服务

时间:2011-07-11 09:27:24

标签: android service android-activity

我有一个位置服务,可以在我的应用中获得更新的位置。我将它绑定到需要位置数据的每个活动,现在我想在服务中的位置监听器接收诸如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);
    }
}

1 个答案:

答案 0 :(得分:1)

我会这样做:

  1. 创建LocationListener类..
  2. 每当我获得一个新位置时,发送带有特定预定义ACTION的广播消息,并在附加内容中输入lat和lon。
  3. 每个活动都使用intentFilter(Action);
  4. 为此消息创建广播侦听器
  5. 从意图中获取额外内容。