2011-12-08 93 views
-1

我有這個按鈕,我想爲它設置一個定時器,以便用戶不必每次都點擊一次,這樣它會自動每隔20秒點擊一次該按鈕。我如何設置它?一個定時器,每20秒自動點擊一次該按鈕

基本上我使用的是tabhost活動,所以共有3個選項卡。在第一個選項卡中,有這個按鈕,我需要單擊該按鈕,因此我可以從Web服務檢索信息,並且每次都會更新此Web服務。當我點擊其他選項卡並返回到第一個選項卡時,我希望它是自動刷新..而不是單擊按鈕來刷新。

  holder.btnClick.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
      } 
}); 

回答

1

既然你有一個按鈕,我假設你在某個地方有一個ActionPerformed類型的方法。

鑑於這種情況,你可以這樣做:

public class AutoClick extends Thread { 
    // Time to wait in milliseconds 
    private long wait; 

    //Latency excepted 
    private long lat; 

    AutoClick(long time, long latency) { 
    wait = time; 
    lat = latency; 
    } 

    public void run() { 
    long start = System.getCurrentTimeMillis(); 

    long current; 
    while(true) 
     current = System.getCurrentTimeMillis(); 
     long step = (current-start) % 20000; 
     if(step <= latency || step >= wait-latency) 
     //call the action-performed method 
    } 
} 

然後創建線程的實例,並運行它:

public AutoClick clicker = new AutoClick(20000); 

clicker.run(); 
+0

嘿,再次看到我的密碼。我希望這個按鈕使用定時器自動點擊。我應該放什麼? – sowhat

2

更有效的方式IMO是使用ScheduledExecutorService

private void doTheActualJobWhenButtonClicked() { 
    // put whatever you need to do when button clicked here 
    ... ... 
} 

... ... 

holder.btnClick.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    // job triggered by user click button: 
    doTheActualJobWhenButtonClicked(); 
    } 
}); 

... ... 

ScheduledExecutorService scheduleTaskExecutor= Executors.newScheduledThreadPool(1); 

// This schedule a task to run every 20 seconds: 
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { 
    public void run() { 
    // job triggered automatically every 20 seconds: 
    doTheActualJobWhenButtonClicked(); 
    } 
}, 0, 20, TimeUnit.SECONDS); 

更新: 如果您的按鈕單擊執行一些UI更新fo R實施例刷新文本在一個TextView,後來乾脆換 內runOnUiThread()的方法調用:

private void doTheActualJobWhenButtonClicked() { 
    myTextView.setText("refreshed"); 
} 

ScheduledExecutorService scheduleTaskExecutor= Executors.newScheduledThreadPool(1); 
// This schedule a task to run every 20 seconds: 
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { 
    public void run() { 
    // involved your call in UI thread: 
    runOnUiThread(new Runnable() { 
     public void run() { 
     doTheActualJobWhenButtonClicked(); 
     } 
    });   
    } 
}, 0, 20, TimeUnit.SECONDS); 

而且你需要打開下一個活動之前,正常關機ScheduledExecutorService的或關閉當前的活動:

// Shut down scheduled task before starting next activity 
if (scheduleTaskExecutor != null) 
    scheduleTaskExecutor.shutdownNow(); 
Intent intent = new Intent(getBaseContext(), NextActivity.class); 
startActivity(intent); 

... ... 

public void onDestroy() { 
    super.onDestroy(); 
    // Shut down scheduled task when closing current activity 
    if (scheduleTaskExecutor != null) 
    scheduleTaskExecutor.shutdownNow(); 
} 

希望這個幫助。

+0

這很好,乾淨,但我認爲使用自創的Thread線程會更靈活,因爲您可以動態更改費率和延遲等。 – Jon

+0

嘿,再次看到我的密碼。我發佈了它。我希望按鈕每隔20秒自動點擊一次,無需點擊它。 – sowhat

+0

@sowhat,看到更新的答案。 – yorkw