每5分钟后如何获得位置?

时间:2013-02-01 06:32:16

标签: android

我正在使用this link进行位置服务,而且工作正常

现在我想创建一个BackgroundService来调用每5分钟后获取位置的函数。 我想我需要使用Timer,请告诉我如何管理这个位置类被调用之间的这5分钟差距。

2 个答案:

答案 0 :(得分:1)

public class LocationService extends Service {      
    private Timer timer; 
    private  long UPDATE_INTERVAL ;
    public static final String Stub = null;
    LocationManager mlocmag;
    LocationListener mlocList ;
    private double lat,longn;

    @Override
    public void onCreate() {
        super.onCreate();
        webService = new WebService();
         mlocmag = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         mlocList = new MyLocationList();

        Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocmag.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        timer  = new Timer();       // location.
        UpdateWithNewLocation(loc); // This method is used to get updated
        mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocList);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
        mlocmag.removeUpdates(mlocList);
    }

    @Override
    public boolean stopService(Intent name) {
        return super.stopService(name);
    }

    private void UpdateWithNewLocation(final Location loc) {
        final SharedPreferences prefs = getSharedPreferences(Const.COMMON_SHARED, Context.MODE_PRIVATE);
        userId = prefs.getString(Const.COMMON_USERID, null);
        gps = prefs.getInt(Const.COMMON_GPS, 0);

        UPDATE_INTERVAL = 500000;

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
        if (loc != null) {
            final double latitude = loc.getLatitude(); // Updated lat
            final double longitude = loc.getLongitude(); // Updated long

            String response = null ;
                if (lat != latitude || longn != longitude ) {

                    response = webService.updateLatandLong(userId, latitude, longitude);
                    lat = latitude;
                    longn = longitude;

                }
        }

        else {
            String latLongStr = "No lat and longitude found";
        }

    }
        }, 0, UPDATE_INTERVAL);
    }


    public class MyLocationList implements LocationListener {

        public void onLocationChanged(Location arg0) {
            UpdateWithNewLocation(arg0);
        }

        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "GPS Disable ",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "GPS enabled",
                    Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

    }
}

答案 1 :(得分:0)

使用此:

Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    public void run() {
//your code to get lat long

                    }
                }, 0, 500000);