I am trying to create a android service that will give updates on the users' location every 5 minutes. I am using the DDMS to send coordinates to the emulator which works fine. I need to convert these coordinates and get the location Eg: New York and using a Toast, print it on screen. I am not using any maps i am trying to use the Geocoder to convert the coordinates supplied by DDMS into the location. I don't get errors but it seems that the coordinates are not being converted to the location and nothing is displayed on screen. Please help have been struggling with this for a very long time. Here is my code. Thanks
public class GetLocationService extends Service {
protected LocationManager locationManager;
Button start;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener ll = new MyLocListener();
Location location = new Location("abc");
ll.onLocationChanged(location );
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
return START_STICKY;
}
private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(getApplicationContext(),
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
try{
Geocoder geo = new Geocoder(GetLocationService.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Log.d("TAG",addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName());
}
Toast.makeText(getApplicationContext(),
addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName(),
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
////Start Service////
public class MyService extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
startService(new Intent(getBaseContext(), GetLocationService.class));
}
}