This is first time I am using service and it sure looks a lot complicated from activity.
So I am trying to get the user's location after he has closed my application with the service.
This is my service class.
public class LocTrack extends Service {
GPSTracker gp;
@Override
public void onCreate()
{ gp = new GPSTracker(getApplicationContext());
onLocationChanged(gp);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onLocationChanged(GPSTracker track) {
// Getting latitude
double latitude = track.getLatitude();
// Getting longitude
double longitude = track.getLongitude();
Geocoder geocoder = new Geocoder(LocTrack.this, Locale.getDefault());
try
{
List<Address> addresses = geocoder.getFromLocation(latitude, longitude,1);
Log.e("Addresses","-->"+addresses);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Please tell me what am I doing my wrong. If I use the code in an activity class then I am able to get the address in the logcat but not when i use service.
This is how i am calling the service from activity
Intent intent = new Intent(MainActivity.this, LocTrack.class);
startService(intent);