In my application i am setting the timer of 10 sec to get the current location by calling locationManager.requestLocationUpdates(); but the problem is that i couldnt get the updated location instead it gives same latitude and longitude where the application has started..
Below is my code written in service.. Help and suggestions appreciated
public class DemoService extends Service{
LocationManager locationManager;
public Timer timer;
public static int i;
private String FILE_NAME = "location.txt";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getApplicationContext(), "onStart()....", Toast.LENGTH_SHORT).show();
timer = new Timer();
timer.schedule(new mainTask(), 0, 10*1000);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "onCreate()....", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy()....", Toast.LENGTH_SHORT).show();
timer.cancel();
}
private class mainTask extends TimerTask
{
public void run()
{
toastHandler.sendEmptyMessage(0);
}
}
private final Handler toastHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Toast.makeText(getApplicationContext(), "test "+(i), Toast.LENGTH_SHORT).show();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListerner());
}
};
public class MyLocationListerner implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
i++;
Toast.makeText(DemoService.this, "\nLat : "+location.getLatitude()+"\nLog : "+location.getLongitude(), Toast.LENGTH_SHORT).show();
saveToFile(location);
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(DemoService.this,"Got onLocationChanged..",Toast.LENGTH_SHORT).show();
System.out.println("Got onStatusChanged");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(DemoService.this,"Got onLocationChanged..",Toast.LENGTH_SHORT).show();
System.out.println("Got onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(DemoService.this,"Got onLocationChanged..",Toast.LENGTH_SHORT).show();
System.out.println("Got onProviderDisabled");
}