The way that I would typically implement this requirement is using a bound Service implementation, like the one in the Local Service Sample in the SDK Documentation. Obviously you're familiar with the advantage of the Service allowing you to create all the location code only once.
Accessing the Service through Bindings allows the Service to start and stop itself so it isn't running when your application isn't in the foreground (it will die as soon as no more Activities are bound). The key, IMO, to making this work well is to BIND the service in onStart() and UNBIND in onStop(), because those two calls overlap as you move from one Activity to another (Second Activity Starts before the First one Stops). This keeps the Service from dying when moving around inside the app, and only lets the service die when the entire application (or at least any part interested in location) leaves the foreground.
With Bindings, you don't have to pass the Location data in a Broadcast, because the Activity can call methods directly on the Service to get the latest location. However, a Broadcast would still be advantageous as a method of indicating WHEN a new update is available...but this would just become a notifier to the listening Activity to call the getLocation() method on the Service.
My $0.02. Hope that Helps!
Answer from devunwired on Stack Overflowandroid - How do I implement the LocationListener? - Stack Overflow
Error with inner class using android.location.LocationListener
Difference between LocationListener and Fused Location API
Use the Fused Location API.
As the name suggests it's a 'fused' location provider, so it uses a mixture of GPS and network provided location to give you a trade-off between accuracy and battery consumption. It can also be updated independently of the OS, so it has a shorter release cycle for updates.
More on reddit.com13 Monitoring Apps To Monitor & Track Your Children’s Smartphone Activities
Videos
The way that I would typically implement this requirement is using a bound Service implementation, like the one in the Local Service Sample in the SDK Documentation. Obviously you're familiar with the advantage of the Service allowing you to create all the location code only once.
Accessing the Service through Bindings allows the Service to start and stop itself so it isn't running when your application isn't in the foreground (it will die as soon as no more Activities are bound). The key, IMO, to making this work well is to BIND the service in onStart() and UNBIND in onStop(), because those two calls overlap as you move from one Activity to another (Second Activity Starts before the First one Stops). This keeps the Service from dying when moving around inside the app, and only lets the service die when the entire application (or at least any part interested in location) leaves the foreground.
With Bindings, you don't have to pass the Location data in a Broadcast, because the Activity can call methods directly on the Service to get the latest location. However, a Broadcast would still be advantageous as a method of indicating WHEN a new update is available...but this would just become a notifier to the listening Activity to call the getLocation() method on the Service.
My $0.02. Hope that Helps!
I got the same problem and I tried to solve it with the good answer of Devunwired, but I had some troubles. I couldn't find a way to stop the service and when I finished my app the GPS-module was still running. So i found another way:
I wrote a GPS.java class:
public class GPS {
private IGPSActivity main;
// Helper for GPS-Position
private LocationListener mlocListener;
private LocationManager mlocManager;
private boolean isRunning;
public GPS(IGPSActivity main) {
this.main = main;
// GPS Position
mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
// GPS Position END
this.isRunning = true;
}
public void stopGPS() {
if(isRunning) {
mlocManager.removeUpdates(mlocListener);
this.isRunning = false;
}
}
public void resumeGPS() {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
this.isRunning = true;
}
public boolean isRunning() {
return this.isRunning;
}
public class MyLocationListener implements LocationListener {
private final String TAG = MyLocationListener.class.getSimpleName();
@Override
public void onLocationChanged(Location loc) {
GPS.this.main.locationChanged(loc.getLongitude(), loc.getLatitude());
}
@Override
public void onProviderDisabled(String provider) {
GPS.this.main.displayGPSSettingsDialog();
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
This class is used in every Activity which needs the GPS coordinates. Every Activity has to implement the following Interface (needed for the communication):
public interface IGPSActivity {
public void locationChanged(double longitude, double latitude);
public void displayGPSSettingsDialog();
}
Now my main Activity looks like that:
public class MainActivity extends Activity implements IGPSActivity {
private GPS gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gps = new GPS(this);
}
@Override
protected void onResume() {
if(!gps.isRunning()) gps.resumeGPS();
super.onResume();
}
@Override
protected void onStop() {
gps.stopGPS();
super.onStop();
}
public void locationChanged(double longitude, double latitude) {
Log.d(TAG, "Main-Longitude: " + longitude);
Log.d(TAG, "Main-Latitude: " + latitude);
}
@Override
public void displayGPSSettingsDialog() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
and a second one like that:
public class TEST4GPS extends Activity implements IGPSActivity{
private GPS gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.gps = new GPS(this);
}
@Override
public void locationChanged(double longitude, double latitude) {
Log.d("TEST", "Test-Longitude: " + longitude);
Log.d("TEST", "Test-Latitude: " + latitude);
}
@Override
protected void onResume() {
if(!gps.isRunning()) gps.resumeGPS();
super. onResume();
}
@Override
protected void onStop() {
gps.stopGPS();
super.onStop();
}
@Override
public void displayGPSSettingsDialog() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
It's not as beautiful as the solution of Devunwired, but it works for me. cya
Hey guys,
in my studybooks I was adviced to put the following code in my learning project (toppings GPS):
"inner class NoteLocationListener : LocationListener {
override fun onLocationChanged(location: Location?) {
Log.d(javaClass.simpleName, "Empfangene Geodaten:\n$location")
val textview = findViewById<TextView>(R.id.textview_output)
textview.append("\nEmpfangene Geodaten:\n$location")
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
}
val locationListener = NoteLocationListener()"
This code was provided from my university. I checked it many times and got no typo. Unfortunately I get the following error, when I want to start this in my Android Studio project:
"Class 'NoteLocationListener' is not abstract and does not implement abstract member public abstract fun onLocationChanged(p0: Location): Unit defined in android.location.LocationListener"
Unfortunately I dont find the solution through this to continue with my learning book. Would somebody so kind and put me in the right direction? Since this code part was exactly in my book to use and work with + I am too unexperienced, I do not see, what is wrong here.
Thanks a lot!