Several methods of delay execution in Android

Source: Internet
Author: User

The methods of delay execution include:

1, open a new thread, 2, the use of timers, 3, Message processing, 4, the use of Alarmmanager

First, open a new thread

New Thread (New Runnable () {

public void Run () {

Thread.Sleep (XXXX);

Handler.sendmessage (); Tell the main thread to perform a task

}

}). Start

Second, the use of timers

TimerTask task = new TimerTask () {

public void Run () {

Execute the Task

}

};

Timer timer = new timer ();

Timer.schedule (task, delay);

Third, message processing

New Handler (). postdelayed (New Runnable () {

public void Run () {

Execute the Task

}

}, delay);

Four, Alarmmanager, simple timing generally do not use this method

Alarmmanager, as the name implies, is a "reminder", a system-level cue service commonly used in Android, that broadcasts a specified intent for us at specific times. The simple thing is that we set a time, and then at that time, Alarmmanager broadcast a intent we set for us, usually we use pendingintent,pendingintent can be understood as intent package, The simple thing is to add a specified action on the intent. When using intent, we also need to perform startactivity, StartService, or sendbroadcast to make intent useful. And Pendingintent's words are to include this action inside.

Several methods of delay execution in Android

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.