4

I am a newbie in android. I am developing an app in which a particular piece of code executes after every 5 seconds in background.To achieve this I am using a service with timer with a timer task in it. For sometime its working fine but after some indefinite my service is running but timer task stops automatically in android. Here is my code please help. Thanks in advance.

    public void onStart(Intent intent, int startid) {
    //this is the code for my onStart in service class
    int delay = 1000; // delay for 1 sec.

    final int period = 5000; // repeat 5 sec.

    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
                        executeCode();
    }, delay, period);

};
user1508383
  • 95
  • 2
  • 11

3 Answers3

4

In my opinion, you should use AlarmManager with an IntentService to schedule repeating background tasks instead of Timer tasks. A Timer is unreliable and doesn't always work correctly within the Android Framework. Also, a Timer won't execute if the phone is asleep. You can have alarms wake up the phone to execute your code with AlarmManager.

See:

https://developer.android.com/reference/android/app/AlarmManager.html

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/

http://android-er.blogspot.in/2010/10/simple-example-of-alarm-service-using.html

In case of phone restart, you will need to trigger the alarm manager again. See this tutorial for exact instructions on how to do this:

http://www.androidenea.com/2009/09/starting-android-service-after-boot.html

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • I am using alarm manager it seems to work fine only problem i am facing is I want to keep running the code even if my phone restarts.How to do it using alarm manager? Do i need to implement boot reciever or any other sort of thing? Please help.Thanks – user1508383 Nov 01 '12 at 06:43
1

Generally TimerTask stops when device goes to sleep mode for long time. Try to use AlarmManager class for your requirement. AlarmManager uses lesser battery consumption too.

Here is an example, how to use AlarmManager

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • I am using alarm manager it seems to work fine only problem i am facing is I want to keep running the code even if my phone restarts.How to do it using alarm manager? Do i need to implement boot reciever or any other sort of thing? Please help.Thanks – user1508383 Nov 01 '12 at 06:42
  • yes you can create a BroadcastReceiver that will start on phone's boot up and active the AlarmManager, i use same for my requirement. – Lucifer Nov 01 '12 at 15:03
0

I guess you can attain this task better if you use CountDown Timer which has an inbuilt method which is called after time you have specified

Example

public class CountDownTest extends Activity {
TextView tv; //textview to display the countdown
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
//5000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000,1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText(”done!”);
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);
}
}

Edit
you can perform any function in OnTick method which will be called in every 1000 millisecond in above example

Learn more about it here

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Just Variable
  • 892
  • 10
  • 19
  • 1
    Thanks Deepak.I know this but i want to implement this in service. So that it runs in background that too contiously. Please help. – user1508383 Oct 31 '12 at 13:08
  • You must create your CountDownTimer class and extend it from Service class. By doing so your activity becomes a service and it runs in the background even if you close the application or the screen it doesnt matter it just does the task that you assign to it in the background . – Just Variable Oct 31 '12 at 13:50