2015-03-25 124 views
0

我有一個檢測位置的代碼,我只想工作2分鐘。僅在特定時間運行腳本

當我啓動start()方法的腳本必須工作大約2分鐘。

問題在那裏,如何運行我的腳本只在特定的時間。 我用這個代碼但沒有正確運行。

不從定時器擋火()()方法。時間表()

public class a implements LocationListener{ 
    private LocationManager locationManager; 
    private String provider; 
    private Location lastloc; 
    private Context _context; 

    public a(Context context){ 
     _context = context; 
    } 

    public void start(){ 
     locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE); 


     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, (LocationListener) this); 

     new Timer().schedule(
      new TimerTask(){ 
       public void run() { 
        stop(); 
       } 
      } 
     ,System.currentTimeMillis(), 2*60*1000); 
    } 

    public void stop(){ 
     Log.d("states","stop"); 
     locationManager.removeUpdates((LocationListener) this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("states", "onLocationChanged()"); 
     lastloc = location; 
    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
    } 

    @Override 
    public void onProviderEnabled(String arg0) { 
    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    } 
} 
+0

你的代碼的行爲如何?它會在2分鐘後繼續更新還是在此之前停止更新? – hasan83 2015-03-25 07:50:23

+0

它在2分鐘後不斷更新並且不停止。 – saeid 2015-03-25 08:23:28

+0

我相信你的問題是不是在這個代碼 – hasan83 2015-03-25 08:48:09

回答

0

再次檢查您的Timer代碼。通常情況下,Timer代碼應該是這樣的:

Timer.schedule(TimerTask, 
     delayTime); // delay a task before executed for the first time, in milliseconds 

因爲你delayTime已通過System.currentTimeMillis()執行時,System採自午夜以毫秒爲單位的當前時間,從而使TimerTask將經過幾百毫秒執行。

因此,使用此代碼:

Timer timer = new Timer(); 
timer.schedule(new TimerTask(){ 
     @Override 
     public void run() { 
      // do your thing here 
     } 
    }, 2*60*1000); 

看到這個documentation你創建的Timer類型。

0

我終於用處理解決我的問題。

閱讀此頁:Using Bundle Android to Exchange Data Between Threads

a.java

public class a implements LocationListener{ 
    private LocationManager locationManager; 
    private String provider; 
    private Location lastloc; 
    private Context _context; 

    private Thread workingthread = null; 
    final Handler mHandler = new Handler(){ 
     public void handleMessage(Message msg) { 
      Log.d("states","return msg from timer2min"); 
      if(msg.what==1){ 
       stop(); 
      } 
      super.handleMessage(msg); 
     } 
    }; 

    public a(Context context){ 
     _context = context; 
    } 

    public void start(){ 
     locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, (LocationListener) this); 

     workingthread=new timer2min(mHandler); 
     workingthread.start(); 
    } 

    public void stop(){ 
     Log.d("states","stop"); 
     locationManager.removeUpdates((LocationListener) this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("states", "onLocationChanged()"); 
    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
    } 

    @Override 
    public void onProviderEnabled(String arg0) { 
    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    } 
} 

timer2min.java

public class timer2min extends Thread { 
    private Handler hd; 

    public timer2min(Handler msgHandler){ 
     hd = msgHandler; 
    } 

    public void run() { 
     try { 
      Thread.sleep(2*60*1000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     Message msg = hd.obtainMessage(); 
     msg.what = 1; 
     hd.sendMessage(msg); 
    } 
}