I have GetURL method defined to find my location from network provider, you can send the code below.
If I define that code piece in my main class with Activity this works nicely but when I would like to create a seperate class (for instance GetLocation class), I cannot use getSystemService method and I receive error in subject (getSystemService is undefined for the type for GetLocation). There are couple of entries regarding this topic but I don’t understand fully.
That’s a rookie question so take this into account while answering 🙂 Thanks guys.
public String GetURL () {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String locationProvider = LocationManager.NETWORK_PROVIDER;
mostRecentLocation = locationManager.getLastKnownLocation(locationProvider);
if(mostRecentLocation != null) {
double lat = mostRecentLocation.getLatitude();
double lng = mostRecentLocation.getLongitude();
currentLocation = "Lat: " + lat + " Lng: " + lng;
Log.e("LOCATION -------------", currentLocation + "");
}
return currentLocation;
}
The method
getSystemService()belongs to theContextclass.Therefore if you want to move
getUrl()to become a utility method elsewhere, you need to pass in aContextobject, such as the currentActivity(as it inherits fromContext).For example:
Util.java
MyActivity.java