2013-04-26 55 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

感谢。对不起,英文不好。