Category: Android
Last week I received my brand new HTC Desire smartphone which is running Android version 2.1. This week I finally got the chance to do some programming on the Android platform. My current employer, IPROFS, has customers that are interested in Android applications, so I am allowed to spend some time to get to know the platform.
If you have no Android experience yet, I recommend viewing the Android introduction movies first, they provide an excellent overview of the Android platform and the ideas behind it.
Now you know the basics, we can do some coding.
For the current application I`m working on I need to retrieve the location of the user. In this article I will explain two possibilities for retrieving the users location. The first one is through the network provider and the second one is through GPS.
Location service
To be able to do anything with locations we need to get a reference to the systems location manager. This can be done by retrieving the LOCATION_SERVICE system service.
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE)
With the LocationManager you can retrieve information like the last know location for example.
Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); ... String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider);
In order to receive information about a location we need to tell the LocationManager that we want to receive location updates. When the LocationManager finds information about a (new) location a notification is send to anyone who is listening to location update events. We can tell the LocationManager what location provider it should use retrieving Location data, this can be either the network provider or a GPS device. In this example I`m using the LocationManager.NETWORK_PROVIDER but you can also use the LocationManager.GPS_PROVIDER.
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener);
JavaDoc of LocationManager.requestLocationUpdates(provider, minTime, minDistance, listener)
- @param provider the name of the provider with which to register
- @param minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
- @param minDistance the minimum distance interval for notifications, in meters
- @param listener LocationListener whose onLocationChanged(Location) method will be called for each location update
Location Listener
We need to declare a LocationListener in order to received updates. Below is an example implementation of a LocationListener. In the public void onLocationChanged(Location location) we receive updates of the location changes.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
...
}
Retrieving the Address
Once we receive a Location object we can get additional information like the address belonging to the location. The Location contains the longitude and latitude of a specific Location, the Geocoder class can help use converting a longitude/latitude to an Address.The helper function below implements this functionality, it searches for the first available Address belonging to a longitude/latitude combination.
Be aware that you need a data connection for retrieving the Address information! The Location can be read either via GPS or your Network Provider but the Address will be retrieved from a remote service.
/**
* Determines the the closest Address for a given Location.
*
* @param context the context
* @param location the location for which an Address needs to be found
* @return and Address or null if not found
* @throws IOException if the Geocoder fails
*/
public Address getAddressForLocation(Context context, Location location) throws IOException {
if (location == null) {
return null;
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int maxResults = 1;
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
if (addresses.size() == 1) {
return addresses.get(0);
} else {
return null;
}
}
Conclusion
Reading location information from your Android smartphone is pretty easy. Once you have the location you can start implementing location aware applications. The only limitation is your creativity
Have fun!
Recently I started exploring Google Android. Android is an open source mobile platform for devices like smartphones, settop boxes and even cars. What I did not know is that the application logic of an Android application can be written in Java.
Basically a Android application consists of:
- Java code: for your application logic;
- XML: for defining the view;
- Resouces: like images, icons, media.
For me the Java part is easy, since I write Java code almost every day (even in the weekends
). But I was struggling with the creation of the view. Google provides plugins for Eclipse so you can build, preview and run you application on an Android emulator. But this doesn’t work flawless for building your views. After a while you start to get used to creating the view from XML.
Where looking for a better solution I came across the DroidDraw application. This is a Java application that allows you to visually design an Android application. If you want to run the application standalone you can download the jar file here.
My first application will be the Yathzee application for Android. After I have receive my new HTC Desire Android phone, I will soon release a first version of the application using the Yathzee Computer as an example:
If you want to get started with Android, I recommend reading the following articles on the android.com developers website:
http://developer.android.com/guide/topics/fundamentals.html
http://developer.android.com/guide/tutorials/hello-world.html

