如何杀死TimerTask实现LocationListener?

时间:2017-01-16 17:54:26

标签: android android-service

stopService不会杀死TimerTask,即使LiveTrackingService停止,计时器仍会在后台每10秒运行一次。

Intent intent = new Intent(getActivity().getApplicationContext(), LiveTrackingService.class);
getActivity().stopService(intent);

这是LiveTrackingService :(我希望每10秒获取一次位置更新,直到用户停止服务。)

public class LiveTrackingService extends Service {

    final Handler mHandler = new Handler();
    private Timer mTimer = new Timer();
    protected LocationManager locationManager;

    public static final long NOTIFY_INTERVAL = 10 * 1000;

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

    @Override
    public void onCreate() {
        //schedule task
        startService();
    }


    private void startService()
    {
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
        mTimer.purge();
        mTimer=null;
        mHandler.removeCallbacksAndMessages(null);
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }


    private class TimeDisplayTimerTask extends TimerTask implements LocationListener {

        @Override
        public void run() {

                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        if(checkLocationPermission()) {
                           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0, TimeDisplayTimerTask.this);

                        }
                    }
                });
        }

        @Override
        public void onLocationChanged(Location location) {
            // I get location and do work here

            if (location != null) {
                Log.d("onLocationChanged", "onLocationChanged");
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

    }

2 个答案:

答案 0 :(得分:1)

保留服务类中的TimeDisplayTimerTask实例,然后使用它删除onDestroy()中的位置更新。

public class LiveTrackingService extends Service {

    private TimeDisplayTimerTask mTask;

    @Override
    public void onCreate() {
        mTask = new TimeDisplayTimerTask();
    }

    private void startService() {
        mTimer.scheduleAtFixedRate(mTask, 0, NOTIFY_INTERVAL);
    }

    @Override
    public void onDestroy() {
        // Remove location updates.
        locationManager.removeUpdates(mTask);
    }
}

答案 1 :(得分:0)

public class LiveTrackingService extends Service {

    final Handler mHandler = new Handler();

    private LocationManager locationManager;
    private TimeDisplayTimerTask mTimerTask=new TimeDisplayTimerTask();
    private Timer mTimer = new Timer();

    public static final long NOTIFY_INTERVAL = 10 * 1000;

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

    @Override
    public void onCreate() {
        startService();
    }


    private void startService()
    {
        mTimer.scheduleAtFixedRate(mTimerTask, 0, NOTIFY_INTERVAL);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
        mTimer.purge();
        mTimer=null;
        mHandler.removeCallbacksAndMessages(null);

        // Remove location updates.
        if(checkLocationPermission()) {
            locationManager.removeUpdates(mTimerTask);
        }

        mTimerTask.cancel();
        mTimerTask=null;

        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }


    class TimeDisplayTimerTask extends TimerTask implements LocationListener {

        @Override
        public void run() {

                mHandler.post(new Runnable() {

                    @Override
                    public void run() {

                        if(checkLocationPermission()) {
                            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0, mTimerTask);

                        }
                    }
                });
        }

        @Override
        public void onLocationChanged(Location location) {
            // I get location and do work here

            if (location != null) {
                Log.d("onLocationChanged", "onLocationChanged");
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

    }