0

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));

  }
}

enter image description here

Sindu_
  • 1,347
  • 8
  • 27
  • 67
  • Are your log statements being executed? Also, have you tied changing "GetLocationService.this" to "getApplicationContext()" in Toast.makeText() because maybe your toast isn't being sent to the right place. – barbiepylon Jul 10 '12 at 18:04
  • @user1454749 Please check my edit. I have tried changing it to "GetLocationService.this" – Sindu_ Jul 10 '12 at 18:26
  • Do you know 100% that your service is being started? Are you sure that the OnStartCommand() method is called? – barbiepylon Jul 10 '12 at 18:38
  • Yes I am sure that the service is started i also checked in the settings->applications->running services and the service was running – Sindu_ Jul 10 '12 at 18:40
  • Can you post the code that shows how you start the service – barbiepylon Jul 10 '12 at 18:44
  • can you try changing "getBaseContext()" to "this" or "MyService.this" when you call startService(). Also, add some log statements to your onStartCommand override to see if that is getting executed. – barbiepylon Jul 10 '12 at 18:54
  • Tried that. Ya the log message gets printed in log cat – Sindu_ Jul 10 '12 at 19:13
  • 1
    I should try it on a real device – Sindu_ Jul 10 '12 at 19:14

1 Answers1

0

Alright, so are your Log statements coming through? Also, Your toast is not going to show, because the context your are passing it is the service context.. The service is not part of the user interface.. therefore the service context is not being displayed. the application context may work... but you really should use the current activity's context.

Joel
  • 4,732
  • 9
  • 39
  • 54
  • No the log statements are not coming through as well. i have uploaded a picture of the log cat. – Sindu_ Jul 10 '12 at 18:27
  • The Geocoder class requires a backend service that is not included in the core android framework. Do you have that? – Joel Jul 10 '12 at 18:30
  • Are you sure? because I have tried using the Geocoder class before and it worked for me without a any backend service. I tried using it in the code i have given above and it doesn't work. – Sindu_ Jul 10 '12 at 18:33
  • Well I'm pretty sure that used to be the case.. could certainly have changed, my bad. I would give it a try on a real device.. – Joel Jul 10 '12 at 18:38