I have an assignment for Android Development in Java, with the Google Maps api.
I have the Maps Activity and a separate location service that tracks the device's location, and i want to add a google maps tracker when the location of the device changes. I can do that if i pass the GoogleMap mMap object into the locationListener class i created:
public class LocationListener implements android.location.LocationListener
{
private Location cur_location;
private LocationManager locationManager;
private GoogleMap mMap;
@SuppressLint("MissingPermission")
public LocationListener(LocationManager locationManager , GoogleMap mMap)
{
this.locationManager = locationManager;
this.mMap = mMap;
this.cur_location = getLastKnownLocation();
}
@SuppressLint("MissingPermission")
private Location getLastKnownLocation() {
List<String> providers = locationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = locationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
@Override
public void onLocationChanged(@NonNull Location location)
{
Log.e("LocationListener" , location.toString());
cur_location = location;
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude() , location.getLongitude())).title("Marker"));
}
public Location getCur_location()
{
return cur_location;
}
}
problem is that the LocationListener is initialised in the Service but the Location from the function getCur_location()
needs to be accessed in the Activity therefore i need to pass the GoogleMap mMap object from the Activity to the Service.
The only way i know how to do that is through the Intent Extras , but the GoogleMap class doesn't implement Serializable nor Parcable.
Is there another way to pass an Object from an Activity to a Service ?
CodePudding user response:
I am assuming that the LocationListener is used within a Service class which you do not include in your question. You can achieve passing an object to the service if the service is a bound service. Check this documentation
Before proceeding explaining how, I want to note that something sounds wrong in this and needs deeper thought on how else you could avoid it
Within your service create a variable to hold the map object and then create an inner class extending the Binder
and create an object of itlike below
private GoogleMap mMap = null;
private LocalBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public void setMap(GoogleMap map) {
mMap = map;
}
}
Then make sure you clear the mMap when the service is not bound anymore
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
mMap = null;
return true;
}
Now all you have to do is in your activity create a serviceConnection and bind to the service.
private MyService.LocalBinder mServiceBinder = null;
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mServiceBinder = (MyService.LocalBinder) service);
// Now you can use the mServiceBinder to call setMap to pass your object to the service
}
};
You can bind anywhere you need (eg inside your onCreate) using the following code
Intent intent = new Intent(this, MyService.class);
boolean bound = getApplicationContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);