Note that there are some explanatory texts on larger screens.

plurals
  1. POListening to bound location service from an activity
    primarykey
    data
    text
    <p>I've got a location service to have an updated location in my app. I bind it to every activity requiring location data, and now I'd like to know in these activities when the location listener in the service receives events such as onLocationChanged, onProviderEnabled... How can I do that?</p> <p>In my activities</p> <pre><code>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); } </code></pre> <p>LocationService.java</p> <pre><code>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 &amp;&amp; location_network !=null) { return (location_gps.getTime() &gt; location_network.getTime())?location_gps:location_network; } else if(location_gps==null &amp;&amp; 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload