2013-04-26 57 views
2

我想創建一個可以每15分鐘檢查一次所需文件的安卓服務。Android服務中的計時器

爲此,我創建了一個示例程序,每10秒鐘使用TTS播放一個文本。並且還使用警報管理器每30秒鐘撥打一次服務

該服務完美呼叫,即使TTS在第一次完美播放,但在50秒後再次呼叫該服務時,計時器並非從0開始相反,從11,12,13開始 - 即使我給了cancel()。

有人能幫我解決這個問題嗎?

以下是在代碼:

public class ServiceLocation extends Service implements OnInitListener 
{ 

TextToSpeech talker; 
Timer t; 
public int time = 0; 

    @Override 
public void onCreate() 
{ 
    super.onCreate(); 
    Log.e("Location1", "Inside onCreate"); 
} 

public void onStart(Intent intent, int startId) 
{ 
    super.onStart(intent, startId); 

    t = new Timer(); 
    Log.e("Location1", "Inside onStart"); 

    talker = new TextToSpeech(this, this); 
    testMethod(); 
} 

public void testMethod() 
{  
    //Set the schedule function and rate 
    t.scheduleAtFixedRate(new TimerTask() { 

     @Override 
     public void run() { 

      time += 1; 
      String todis = String.valueOf(time); 


      if(todis.contains("20")) 
      { 

       talker.speak("Testing Service in a App",TextToSpeech.QUEUE_ADD,null); 

       t.cancel(); 
       t.purge(); 
      } 
     } 

    }, 0, 1000); 
} 

    public void onInit(int status) 
    {    
    talker.speak("Testing Service in a App",TextToSpeech.QUEUE_ADD,null); 
    } 
} 

回答

0

TimerTask的複習;

    timer = new Timer();  
       refresher = new TimerTask() { 
        public void run() { 


         //your code 

        }; 
       }; 
       first event immediately, following after 1 seconds each 
       timer.scheduleAtFixedRate(refresher, 0,1000); 

//您可以更改1000作爲您所需的時間。 1000等於1秒。

你可以把這段代碼放在你的服務類中oncreate ..你可以調用你想調用的代碼段中每15分鐘調用一次的方法。

希望這對你有所幫助。

0

您必須每10秒重置您的計時器。

示例代碼:

public void startTimer(){ 
    t = new Timer(); 
    task = new TimerTask() { 

    @Override 
    public void run() { 
    runOnUiThread(new Runnable() { 

     @Override 
    public void run() { 
     TextView tv1 = (TextView) findViewById(R.id.timer); 
     tv1.setText(time + ""); 
     if (time > 0) 
     time -= 1; 
     else { 
     tv1.setText("Welcome");   
     } 
    } 
    }); 
    } 
    }; 
    t.scheduleAtFixedRate(task, 0, 1000); 
} 

爲breif例如看到我blog post

我希望這會幫助你。

1

在您的onStart()方法中,您正在初始化Timer()但您必須檢查計時器是否正在運行?如果它正在運行,則取消它並啓動一個新的計時器。下面是示例代碼:

public static final long NOTIFY_INTERVAL = YOUR_REQUIRED_INTERVAL_IN_SECODE* 1000; 

// run on another Thread to avoid crash 
private Handler yourHandler = new Handler(); 
// timer handling 
private Timer yourTimer = null; 

@Override 
public void onCreate() { 
    // cancel if already existed 
    if(yourTimer != null) { 
     yourTimer.cancel(); 
    } 
     // recreate new 
     yourTimer = new Timer(); 

    // schedule task 
    yourTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL); 
} 

這裏是你的TimeDisplayTimerTask()

class TimeDisplayTimerTask extends TimerTask { 

    @Override 
    public void run() { 
     // run on another thread 
     yourHandler.post(new Runnable() { 

      @Override 
      public void run() { 
       // display toast 
       Toast.makeText(getApplicationContext(), "some message", 
         Toast.LENGTH_SHORT).show(); 
      } 

     }); 
    } 

要取消計時器,你可以叫這個

if(yourTimer != null) { 
      yourTimer.cancel(); 
     }` 

注:

  1. 固定速率計時器(scheduleAtFixedRate())基於開始時間(因此每個迭代將在startTime + iterationNumber * delayTime處執行)。 Link here
  2. 要了解有關計劃和定時器任務然後查看this Link

感謝。對不起,英文不好。