Monday 30 March 2015

Run repeatedly task using timer class in android.

Performing repeated tasks in a Service

Besides performing long-running tasks in a service, you might also perform some repeated tasks in a service. For example, you may write an alarm clock service that runs persistently in the background. In this case, your service may need to periodically execute some code to check whether a prescheduled time has been reached so that an alarm can be sounded. To execute a block of code to be executed at a regular time interval, you can use the Timer class within your service.

The following Example shows you how.

Running Repeated Tasks Using the Timer Class

1 . Using the same project created in the previous post, add the following statements in bold to the MyService.java file:

package ​com.emergingandroidtech.Services;
import ​android.app.Service;
import​ android.content.Intent;
import​ android.os.AsyncTask;
import​ android.os.IBinder;
import​ android.util.Log;
import​ android.widget.Toast;
import ​java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

public ​class ​MyService​ extends​ Service​
{
​​​​int counter = 0;
​​​​static final int UPDATE_INTERVAL = 1000;
​​​​private Timer timer = new Timer();
​​​​
@Override
​​​​public​ IBinder ​onBind(Intent ​arg0)
​{
​​​​​​​​return ​null;
​​​​}
​​​​
@Override
​​​​public ​int ​onStartCommand(Intent ​intent,​int ​flags,​int ​startId)​
{
​​​​​​​​//​We ​want​ this ​service​ to ​continue​r unning ​until ​it ​is ​explicitly
​​​​​​​​//​stopped,​so​r eturn ​sticky. ​
​​​​​​​Toast.makeText(this,​“Service​Started”,​Toast.LENGTH_LONG).show();
​​​​​​​​
doSomethingRepeatedly();
​​​​​​​​
return ​START_STICKY; ​​​​
}
​​​​
private void doSomethingRepeatedly()
{
​​​​​​​​timer.scheduleAtFixedRate( new TimerTask()
{
​​​​​​​​​​​​public void run()
{
​​​​​​​​​​​​​​​​Log.d(“MyService”, String.valueOf(++counter));
​​​​​​​​​​​​}
​​​​​​​​},
0,
UPDATE_INTERVAL);
​​​​}
​​​​
@Override
​​​​public ​void ​onDestroy()​
{ ​
​​​​​​​super.onDestroy();
​​​​​​​​if (timer != null)
{
​​​​​​​​​​​​timer.cancel(); ​
​​​​​​​}
​​​​​​​​Toast.makeText(this,​“Service​Destroyed”,​Toast.LENGTH_LONG).show(); ​​
​​}
}

2 . Click the Start Service button. 

3 . Observe the output displayed in the LogCat window: 

01-16​15:12:04.364:​DEBUG/MyService(495):​1
01-16​15:12:05.384:​DEBUG/MyService(495):​2
01-16​15:12:06.386:​DEBUG/MyService(495):​3
01-16​15:12:07.389:​DEBUG/MyService(495):​4
01-16​15:12:08.364:​DEBUG/MyService(495):​5
01-16​15:12:09.427:​DEBUG/MyService(495):​6
01-16​15:12:10.374:​DEBUG/MyService(495):​7

How It Works

In this example, you created a Timer object and called its scheduleAtFixedRate() method inside the doSomethingRepeatedly() method that you have defined:

​​​​private ​void ​doSomethingRepeatedly()​
{
​​​​​​​​timer.scheduleAtFixedRate(new​TimerTask()​
{
​​​​​​​​​​​​public ​void ​run()​
{
​​​​​​​​​​​​​​​​Log.d(“MyService”,​String.valueOf(++counter));
​​​​​​​​​​​​}
​​​​​​​​},
​0,
​UPDATE_INTERVAL);
​​​​}

You passed an instance of the TimerTask class to the scheduleAtFixedRate() method so that  you can execute the block of code within the run() method repeatedly. The second parameter to the scheduleAtFixedRate() method specifies the amount of time, in milliseconds, before first execution. The third parameter specifies the amount of time, in milliseconds, between subsequent executions. In the preceding example, you essentially print out the value of the counter every one second (1,000 milliseconds). The service repeatedly prints the value of counter until the service is terminated:

​​​​@Override ​
​​​public ​void ​onDestroy()​
{
​​​​​​​​super.onDestroy();
​​​​​​​​if​(timer​!=​null)
{
​​​​​​​​​​​​timer.cancel(); ​​​
​​​​​} ​
​​​​​​​Toast.makeText(this,​“Service​Destroyed”,​Toast.LENGTH_LONG).show(); ​​
​​}

For the scheduleAtFixedRate() method, your code is executed at fixed time intervals, regardless of how long each task takes. For example, if the code within your run() method takes two seconds to complete, then your second task will start immediately after the first task has ended. Similarly, if your delay is set to three seconds and the task takes two seconds to complete, then the second task will wait for one second before starting.

No comments:

Post a Comment