Android Development Step-by-step 59:android scheduled Task Scheduledthreadpoolexecutor

Source: Internet
Author: User

Android timed tasks have a variety of, 1, Timer+timertask 2, Handler.postdelay 3, Alarmmanager 4, Scheduledthreadpoolexecutor, the front 3 are more common, I believe that we also often use, this article introduces the use of multi-threaded scheduledthreadpoolexecutor, it compared to JDK 1.5 of the advantages of the timer has several: 1, the use of multi-threading, timer is a single thread, Once the timer inside any one timertask abnormal words, the entire timer thread will stop 2, the timer depends on the computer's own time, such as the appointment 10 minutes after the task, the computer time forward for 5 minutes, then the task began to execute 5 minutes later, The scheduledthreadpoolexecutor uses relative time, regardless of the computer time before or after the adjustment, not affected. jdk1.5 is recommended after the use of Scheduledthreadpoolexecutor, to start our demo, our demo is to do a stopwatch, every second to perform a task, and then update the TextView number. Please see demo:

<span style= "FONT-SIZE:14PX;" >/** * */package Com.figo.study;import Java.util.concurrent.scheduledthreadpoolexecutor;import Java.util.concurrent.timeunit;import Com.figo.study.utils.stringutils;import Android.app.Activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.view.view;import Android.view.view.onclicklistener;import android.widget.button;import android.widget.textview;/** * @author Figo * */ public class Scheduledthreadpoolexecutoractivity extends Activity {TextView tv_time; Handler Handler; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//update time Handler = new Handler () {public void H Andlemessage (Message msg) {switch (msg.what) {case 1:tv_time.settext (Msg.getdata (). getString ("num")); Super.handlemessage (msg);}}; Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_scheduledthreadpool); Button Btnshedule = (button) Findviewbyid (r.id.btnschedule); tv_time = (TextView) Findviewbyid (r.id.tv_time); BtnshEdule.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Scheduledthreadpoolexecutor Scheduledthreadpoolexecutor = new Scheduledthreadpoolexecutor (1); Runnable command = new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubif (stringutils.isnotempt Y (Tv_time.gettext (). ToString ())) {int time = Integer.parseint (Tv_time.gettext (). ToString ()); time = time + 1; Message message = new Message (); message.what = 1; Bundle data = new Bundle ();d ata.putstring ("num", string.valueof (Time)), Message.setdata (data); Handler.sendmessage ( message),//In the non-UI thread update is not valid, must be in the outside UI main thread update//Tv_time.settext (string.valueof (time));} else {//Tv_time.settext ("0");}}};/ /java.util.concurrent.scheduledthreadpoolexecutor.scheduleatfixedrate//(Runnable command, long InitialDelay, long Period, Timeunit Unit) scheduledthreadpoolexecutor.scheduleatfixedrate (command, 1, 1,timeunit.seconds);}} </span>

Android Development Step-by-step 59:android scheduled Task Scheduledthreadpoolexecutor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.