{"id":13739,"date":"2014-08-27T15:00:06","date_gmt":"2014-08-27T12:00:06","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=13739"},"modified":"2019-04-24T16:02:22","modified_gmt":"2019-04-24T13:02:22","slug":"android-timertask-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/","title":{"rendered":"Android TimerTask Example"},"content":{"rendered":"<p>In this example, we are going to see, how a <code>java.util<\/code> method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or a fixed rate on Android. We will use the abstract class <a href=\"http:\/\/developer.android.com\/reference\/java\/util\/TimerTask.html\">TimerTask<\/a>.<\/p>\n<p>With the default fixed-period execution, each successive run of a task is scheduled relative to the start time of the previous run, so two runs are never fired closer together in time than the specified period. With fixed-rate execution, the start time of each successive run of a task is scheduled without regard for when the previous run took place. This may result in a series of bunched-up runs (one launched immediately after another) if delays prevent the timer from starting tasks on time.<\/p>\n<p>In Android, Timer and Handler can be used for pretty much the same reasons, and in the same way. Literally, <a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html\">Handler<\/a> is a native android class that handles memory leaks problems in a better way than Java-Timer does. This is why Handler is very common amongst Android programmers and projects. You can also learn more about Android Handler in the JavaCodeGeeks <a href=\"http:\/\/examples.javacodegeeks.com\/android\/core\/os\/handler\/android-handler-example\/\">Android Handler Example<\/a>.<\/p>\n<p>However, in this tutorial we are going to see how to use a TimerTask, and we will make an application that will handle with recurring events. We will use the following tools in a Windows 64-bit platform:<\/p>\n<ul>\n<li>JDK 1.7<\/li>\n<li>Eclipse 4.2 Juno<\/li>\n<li>Android SDK 4.4.2<\/li>\n<\/ul>\n<p>Let\u2019s take a closer look:\n<\/p>\n<h2>1. Create a New Android Application Project<\/h2>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<p>Open Eclipse IDE and go to File \u2192 New \u2192 Project \u2192 Android Application Project.<\/p>\n<p><figure id=\"attachment_13637\" aria-describedby=\"caption-attachment-13637\" style=\"width: 596px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1.png\" alt=\"Figure 2. Create a new Android project\" width=\"596\" height=\"559\" class=\"size-full wp-image-13637\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1.png 596w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1-300x281.png 300w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/a><figcaption id=\"caption-attachment-13637\" class=\"wp-caption-text\">Figure 1. Create a new Android project<\/figcaption><\/figure><\/p>\n<p>Specify the name of the application, the project and the package and then click Next.<\/p>\n<p><figure id=\"attachment_13786\" aria-describedby=\"caption-attachment-13786\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-1.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-1.jpg\" alt=\"Figure 2. Create a new Android project name\" width=\"636\" height=\"543\" class=\"size-full wp-image-13786\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-1.jpg 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-1-300x256.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-13786\" class=\"wp-caption-text\">Figure 2. Create a new Android project name<\/figcaption><\/figure><\/p>\n<p>In the next window, the \u201cCreate Activity\u201d option should be checked. The new created activity will be the main activity of your project. Then press Next button.<\/p>\n<p><figure id=\"attachment_11777\" aria-describedby=\"caption-attachment-11777\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample3.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample3.jpg\" alt=\"Create Activity\" width=\"636\" height=\"545\" class=\"alignnone size-full wp-image-13642\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample3.jpg 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample3-300x257.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-11777\" class=\"wp-caption-text\">Figure 3. Configure the project<\/figcaption><\/figure><\/p>\n<p>In \u201cConfigure Launcher Icon\u201d window you should choose the icon you want to have in your app. We will use the default icon of android, so click Next.<\/p>\n<p><figure id=\"attachment_11778\" aria-describedby=\"caption-attachment-11778\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4.png\" alt=\"Configure Launcher Icon\" width=\"660\" height=\"590\" class=\"alignnone size-full wp-image-13638\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11778\" class=\"wp-caption-text\">Figure 4. Configure the launcher icon<\/figcaption><\/figure><\/p>\n<p>Select the \u201cBlank Activity\u201d option and press Next.<\/p>\n<p><figure id=\"attachment_11779\" aria-describedby=\"caption-attachment-11779\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5.png\" alt=\"Blank Activity\" width=\"660\" height=\"590\" class=\"alignnone size-full wp-image-13639\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11779\" class=\"wp-caption-text\">Figure 5. Create the activity and select its type<\/figcaption><\/figure><\/p>\n<p>You have to specify a name for the new Activity and a name for the layout description of your app. The .xml file for the layout will automatically be created in the res\/layout folder. It will also be created a fragment layout xml, that we are not going to use in this project and you can remove it if you want. Then press Finish.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><figure id=\"attachment_13788\" aria-describedby=\"caption-attachment-13788\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-6.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-6.jpg\" alt=\"Figure 6. Create a new blank activity\" width=\"660\" height=\"590\" class=\"size-full wp-image-13788\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-6.jpg 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-6-300x268.jpg 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-13788\" class=\"wp-caption-text\">Figure 6. Create a new blank activity<\/figcaption><\/figure><\/p>\n<p>You can see the structure of the project:<\/p>\n<p><figure id=\"attachment_13789\" aria-describedby=\"caption-attachment-13789\" style=\"width: 391px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-7.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-7.jpg\" alt=\"Figure 7. The tree of the project\" width=\"391\" height=\"453\" class=\"size-full wp-image-13789\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-7.jpg 391w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-7-258x300.jpg 258w\" sizes=\"(max-width: 391px) 100vw, 391px\" \/><\/a><figcaption id=\"caption-attachment-13789\" class=\"wp-caption-text\">Figure 7. The tree of the project<\/figcaption><\/figure><\/p>\n<p><span id=\"code\"> <\/span><\/p>\n<h2>2. Creating the layout of the main Activity<\/h2>\n<p>We are going to make a very simple layout xml for our activity, that only consists of a <code>LinearLayout<\/code> that contains the necessary textviews and buttons.<\/p>\n<p>Open <code>res\/layout\/activity_main.xml<\/code>, go to the respective xml tab and paste the following:<\/p>\n<p><em><span style=\"text-decoration: underline\">activity_main.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\n&gt;\n   &lt;TextView         \n        android:gravity=\"center\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Android TimerTask Example\"\n        android:textSize=\"25dp\"\n        android:padding=\"20dp\"\/&gt;\n   \n   &lt;TextView         \n        android:gravity=\"center\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Every 10 seconds, shows a toast with the current datetime. If you want to cancel the timer, just tap cancel.\"\n        android:textSize=\"20dp\"\n        android:padding=\"20dp\"\/&gt;\n\n   &lt;Button\n        android:id=\"@+id\/cancel\"\n        android:layout_gravity=\"center\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:onClick=\"stoptimertask\"\n        android:text=\"Cancel\" \/&gt;\n&lt;\/LinearLayout&gt;\n<\/pre>\n<p>In the layout above, we have used the attribute <code>android:paddingBottom<\/code> that uses a variable indicates the value of the padding that we want to have: <code>android:paddingBottom=\"@dimen\/activity_vertical_margin\"<\/code>. In order to set our sizes, in Android, we have the dimens.xml. This xml should be placed in the <code>res\/values\/dimens.xml<\/code> folder. Here we set the dimens for the paddings of our <code>activity_main.xml<\/code> layout.<\/p>\n<p><em><span style=\"text-decoration: underline\">dimens.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;resources&gt;\n\n    &lt;!-- Default screen margins, per the Android Design guidelines. --&gt;\n    &lt;dimen name=\"activity_horizontal_margin\"&gt;16dp&lt;\/dimen&gt;\n    &lt;dimen name=\"activity_vertical_margin\"&gt;16dp&lt;\/dimen&gt;\n\n&lt;\/resources&gt;\n\n<\/pre>\n<h2>3. Creating the source code of the main Activity<\/h2>\n<p>Open <code>src\/com.javacodegeeks.androidtimertaskexample\/AndroidTimerTaskExample.java<\/code> file and paste the code below.<\/p>\n<p><em><span style=\"text-decoration: underline\">AndroidTimerTaskExample.java<\/span><\/em><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.androidtimertaskexample;\n\nimport java.text.SimpleDateFormat;\nimport java.util.Calendar;\nimport java.util.Timer;\nimport java.util.TimerTask;\nimport android.app.Activity;\nimport android.os.Bundle;\nimport android.os.Handler;\nimport android.view.View;\nimport android.widget.Toast;\n\npublic class AndroidTimerTaskExample extends Activity {\n\n\tTimer timer;\n\tTimerTask timerTask;\n\t\n\t\/\/we are going to use a handler to be able to run in our TimerTask\n\tfinal Handler handler = new Handler();\n\n\t@Override\n\tprotected void onCreate(Bundle savedInstanceState) {\n\t\tsuper.onCreate(savedInstanceState);\n\t\tsetContentView(R.layout.activity_main);\n\t}\n\n\t@Override\n\tprotected void onResume() {\n\t\tsuper.onResume();\n\t\t\n\t\t\/\/onResume we start our timer so it can start when the app comes from the background\n\t\tstartTimer();\n\t}\n\n\tpublic void startTimer() {\n\t\t\/\/set a new Timer\n\t\ttimer = new Timer();\n\t\t\n\t\t\/\/initialize the TimerTask's job\n\t\tinitializeTimerTask();\n\t\t\n\t\t\/\/schedule the timer, after the first 5000ms the TimerTask will run every 10000ms\n\t\ttimer.schedule(timerTask, 5000, 10000); \/\/\n\t}\n\n\tpublic void stoptimertask(View v) {\n\t\t\/\/stop the timer, if it's not already null\n\t\tif (timer != null) {\n\t\t\ttimer.cancel();\n\t\t\ttimer = null;\n\t\t}\n\t}\n\n\tpublic void initializeTimerTask() {\n\t\t\n\t\ttimerTask = new TimerTask() {\n\t\t\tpublic void run() {\n\t\t\t\t\n\t\t\t\t\/\/use a handler to run a toast that shows the current timestamp\n\t\t\t\thandler.post(new Runnable() {\n\t\t\t\t\tpublic void run() {\n\t\t\t\t\t\t\/\/get the current timeStamp\n\t\t\t\t\t\tCalendar calendar = Calendar.getInstance();\n\t\t\t\t\t\tSimpleDateFormat simpleDateFormat = new SimpleDateFormat(\"dd:MMMM:yyyy HH:mm:ss a\");\n\t\t\t\t\t\tfinal String strDate = simpleDateFormat.format(calendar.getTime());\n\t\t\t\t\t\n\t\t\t\t\t\t\/\/show the toast\n\t\t\t\t\t\tint duration = Toast.LENGTH_SHORT;\t\n\t\t\t\t\t\tToast toast = Toast.makeText(getApplicationContext(), strDate, duration);\n\t\t\t\t\t\ttoast.show();\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t};\n\t}\n}\n<\/pre>\n<p>Let&#8217;s see in detail the code above.[ulp id=&#8217;Ja8Orb5oPKdShcXt&#8217;]<\/p>\n<p>We set the <code>activity_main.xml<\/code> layout and we initialize our calendarview by:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">setContentView(R.layout.activity_main);<\/pre>\n<p>We start our timer by using the code:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tpublic void startTimer() {\n\t\t\/\/set a new Timer\n\t\ttimer = new Timer();\n\t\t\n\t\t\/\/initialize the TimerTask's job\n\t\tinitializeTimerTask();\n\t\t\n\t\t\/\/schedule the timer, after the first 5000ms the TimerTask will run every 10000ms\n\t\ttimer.schedule(timerTask, 5000, 10000); \/\/\n\t}<\/pre>\n<p>Using the <code>timer.schedule(timerTask, 5000, 10000);<\/code> we schedule a task for repeated fixed-delay, that is the 10000ms and execution after a specific delay, for instance 5000ms.<\/p>\n<p>We stop the timerTask by executing the code:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tpublic void stoptimertask(View v) {\n\t\t\/\/stop the timer, if it's not already null\n\t\tif (timer != null) {\n\t\t\ttimer.cancel();\n\t\t\ttimer = null;\n\t\t}\n\t}<\/pre>\n<p>This ensures that the timer is not already stopped, and then cancels the timerTask.<\/p>\n<p>We have to initialize the main function of the TimerTask. This is done in the lines:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tpublic void initializeTimerTask() {\n\t\t\n\t\ttimerTask = new TimerTask() {\n\t\t\tpublic void run() {\n\t\t\t\t\n\t\t\t\t\/\/use a handler to run a toast that shows the current timestamp\n\t\t\t\thandler.post(new Runnable() {\n\t\t\t\t\tpublic void run() {\n\t\t\t\t\t\t\/\/get the current timeStamp\n\t\t\t\t\t\tCalendar calendar = Calendar.getInstance();\n\t\t\t\t\t\tSimpleDateFormat simpleDateFormat = new SimpleDateFormat(\"dd:MMMM:yyyy HH:mm:ss a\");\n\t\t\t\t\t\tfinal String strDate = simpleDateFormat.format(calendar.getTime());\n\t\t\t\t\t\n\t\t\t\t\t\t\/\/show the toast\n\t\t\t\t\t\tint duration = Toast.LENGTH_SHORT;\t\n\t\t\t\t\t\tToast toast = Toast.makeText(getApplicationContext(), strDate, duration);\n\t\t\t\t\t\ttoast.show();\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t};\n\t}\n<\/pre>\n<p>Here, we have to use a handler, in order to show the toast with the current datetime message, independently from what happens to the other methods of the Activity.<\/p>\n<h2>4. Android Manifest<\/h2>\n<p>The AndroidManifest.xml of our project is simple and basic:<\/p>\n<p><em><span style=\"text-decoration: underline\">AndroidManifest.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false\">    \n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    package=\"com.javacodegeeks.androidtimertaskexample\"\n    android:versionCode=\"1\"\n    android:versionName=\"1.0\" &gt;\n\n    &lt;uses-sdk\n        android:minSdkVersion=\"8\"\n        android:targetSdkVersion=\"19\" \/&gt;\n\n    &lt;application\n        android:allowBackup=\"true\"\n        android:icon=\"@drawable\/ic_launcher\"\n        android:label=\"@string\/app_name\" &gt;\n        &lt;activity\n            android:name=\"com.javacodegeeks.androidtimertaskexample.AndroidTimerTaskExample\"\n            android:label=\"@string\/app_name\" &gt;\n            &lt;intent-filter&gt;\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\n            &lt;\/intent-filter&gt;\n        &lt;\/activity&gt;\n    &lt;\/application&gt;\n&lt;\/manifest&gt;\n<\/pre>\n<h2>5. Build, compile and run<\/h2>\n<p>When we build, compile and run our project, the main Activity should look like this:<\/p>\n<p><figure id=\"attachment_13795\" aria-describedby=\"caption-attachment-13795\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-8.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-8.jpg\" alt=\"This is the a screenshot that shows the Toast, meaning the timer was scheduled to run the TimerTask every 10000ms.\" width=\"400\" height=\"711\" class=\"size-full wp-image-13795\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-8.jpg 400w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-8-168x300.jpg 168w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-13795\" class=\"wp-caption-text\">This is the a screenshot that shows the Toast, meaning the timer was scheduled to run the TimerTask every 10000ms.<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_13796\" aria-describedby=\"caption-attachment-13796\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-9.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-9.jpg\" alt=\"This is another screenshot that shows the recurring event. This screenshot is taken 10 seconds after the first one.\" width=\"400\" height=\"711\" class=\"size-full wp-image-13796\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-9.jpg 400w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample-9-168x300.jpg 168w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-13796\" class=\"wp-caption-text\">This is another screenshot that shows the recurring event. This screenshot is taken 10 seconds after the first one.<\/figcaption><\/figure><\/p>\n<h2>Download the Eclipse Project<\/h2>\n<p>This was an example of Android AndroidTimerTaskExample.<\/p>\n<div class=\"download\"><strong>Download<br \/>\n<\/strong>You can download the full source code of this example here:  <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidTimerTaskExample.zip\"><strong>AndroidTimerTaskExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or a fixed rate on Android. We will use the abstract class TimerTask. With the default fixed-period execution, each successive run of a task is scheduled relative &hellip;<\/p>\n","protected":false},"author":14,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-13739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-activity"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android TimerTask Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android TimerTask Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-27T12:00:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-24T13:02:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Chryssa Aliferi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Chryssa Aliferi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\"},\"author\":{\"name\":\"Chryssa Aliferi\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\"},\"headline\":\"Android TimerTask Example\",\"datePublished\":\"2014-08-27T12:00:06+00:00\",\"dateModified\":\"2019-04-24T13:02:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\"},\"wordCount\":865,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"articleSection\":[\"activity\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\",\"name\":\"Android TimerTask Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2014-08-27T12:00:06+00:00\",\"dateModified\":\"2019-04-24T13:02:22+00:00\",\"description\":\"In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"core\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"activity\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/activity\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Android TimerTask Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\",\"name\":\"Chryssa Aliferi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"caption\":\"Chryssa Aliferi\"},\"description\":\"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android TimerTask Example - Java Code Geeks","description":"In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/","og_locale":"en_US","og_type":"article","og_title":"Android TimerTask Example - Java Code Geeks","og_description":"In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or","og_url":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-08-27T12:00:06+00:00","article_modified_time":"2019-04-24T13:02:22+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Chryssa Aliferi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Chryssa Aliferi","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/"},"author":{"name":"Chryssa Aliferi","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332"},"headline":"Android TimerTask Example","datePublished":"2014-08-27T12:00:06+00:00","dateModified":"2019-04-24T13:02:22+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/"},"wordCount":865,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","articleSection":["activity"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/","name":"Android TimerTask Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2014-08-27T12:00:06+00:00","dateModified":"2019-04-24T13:02:22+00:00","description":"In this example, we are going to see, how a java.util method, can help us deal with timer recurring tasks that are scheduled with either a fixed period or","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/activity\/android-timertask-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"core","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/"},{"@type":"ListItem","position":4,"name":"activity","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/activity\/"},{"@type":"ListItem","position":5,"name":"Android TimerTask Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332","name":"Chryssa Aliferi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","caption":"Chryssa Aliferi"},"description":"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/13739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=13739"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/13739\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=13739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=13739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=13739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}