0

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");
            }
NullPointerException
  • 3,978
  • 4
  • 34
  • 52
  • try this : just add this line : locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, refreshInterval, 50, locationListener); – Mayur Bhola Mar 09 '12 at 06:39
  • There are different ways of fetching current location with high or low accuracy, I see you are using network provider so you may get same location in bit wide range you move, since it gives location of the network tower and not your location. You might need to poll GPS. Check out this post http://stackoverflow.com/a/6775456/28557 – Vinayak Bevinakatti Mar 09 '12 at 06:50
  • Hi mayur its not working and it needs GPS to be turned on. Is it not possible to get location thro network providers... – NullPointerException Mar 09 '12 at 06:52
  • @Vinayak.B vinayak!! can i hv example of how to switch between different providers?? As i am new to android – NullPointerException Mar 09 '12 at 06:59
  • Refer the code from here which is good http://stackoverflow.com/a/3145655/28557 – Vinayak Bevinakatti Mar 09 '12 at 07:03
  • @Vinayak.B- thx. I m working on it. and plz 1 more help plz I wanna log the only new location. So is there any way to compare new co-ordinates with new co-ordinates. – NullPointerException Mar 09 '12 at 09:58

1 Answers1

0

I achieved the resultant effect by CWAC Location poller by which I efficiently got the current location with the help of alarm manager with less consumption of battery

NullPointerException
  • 3,978
  • 4
  • 34
  • 52