2017-01-16 69 views
0

stopService不會終止TimerTask,即使LiveTrackingService已停止,計時器仍會在後臺每10秒運行一次。如何殺死TimerTask實現LocationListener?

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 

     } 

    } 

回答

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

的onDestroy()被說成是不可靠的。無論如何,這似乎是可行的方式來做到這一點。 TimerTask沒有被殺死的原因可能是它運行在它本身的ThreadPool和服務在UI線程中。部署計時器後,服務可能無法控制它。 –

+0

它沒有工作,計時器任務仍在運行。 – arlen

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 

     } 

    } 
+0

你解決了你的問題嗎? – nandsito