I am new to oriented object paradigm and I work on Android using Java project for an internship.
I must be able to locate the user and some around buildings I read stuff about how to setup LocationListener and all, and decided that I better write a class that manage everything for me.
public class NetCampusLocation {
private Activity activity;
private LocationManager locationManager;
private LocationListener locationListener;
private Location freshLocation;
public NetCampusLocation(Activity activity) {
this.activity = activity;
this.locationManager = (LocationManager) this.activity.getSystemService(Context.LOCATION_SERVICE);
this.locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
this.freshLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (this.freshLocation == null) {
this.freshLocation = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
public void setLocationUpdate(String provider, int minTimeInterval, int minDistance, LocationListener listener) {
this.locationManager.requestLocationUpdates(provider, minTimeInterval, minDistance, listener);
}
public void stopLocationUpdate() {
this.locationManager.removeUpdates(this.locationListener);
}
}
That is the first time I am writing a class on my own (without any pedagogic goal) and I ever seen an example with an interface nested to a class, I am wondering if this is even logical or totally absurd.
I am also wondering if this a good practice to pass my activity to the constructor or the class or not.
All kind of advices would be really good.
mLocationManager
,mLocationListener
etc. I may be wrong on this, but I believe this to denote that the variable belongs to the class, so you can quickly type m and get a list of all the variables in your class. \$\endgroup\$