-2

我正在使用此链接进行定位服务并且它有效

现在我想创建 BackgroundService,它调用一个每 5 分钟获取一次位置的函数。我想我需要为此使用计时器,请告诉我如何管理这个位置类被调用之间的 5 分钟间隔。

4

2 回答 2

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

        }

    }
}
于 2013-02-08T12:38:03.290 回答
0

用这个:

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

                    }
                }, 0, 500000);
于 2013-02-01T06:51:53.980 回答