Android uses service background update Schedule Task

Source: Internet
Author: User
Tags garbage collection stub time in milliseconds

Service is one of the four components of Android, there is no longer too much to describe, the following main implementation of the use of the service background to perform the task of planning, exit the application, close service, only exist throughout the application cycle.

First use of service needs to be registered in the application in manifest

<service android:name= ". Wxservice "android:enabled=" true "android:exported=" true ">

</service>

Write a Wxservice class that inherits from the service and then implements some of its methods.

public class Wxservice extends service{
    
    @Override public
    ibinder onbind (Intent Intent) {
        //TODO auto-generated method stub return
        null;
    }
    
    @Override public
    void OnCreate () {
        //TODO auto-generated method Stub
        super.oncreate ();
        Timer timer = new timer ();
        Timer.schedule (New Work (), 0, 30000);
            
    }
    
    @Override public
    void OnStart (Intent Intent, int startid) {
        //TODO auto-generated method stub
        Super.onstart (Intent, Startid);
    }

The schedule is scheduled in the OnCreate () method, and this schedule class is described below.

public void Schedule (timertask task, long delay, long period) schedules the SPE Cified task for repeated fixed-delay execution, beginning after the specified delay.
Subsequent executions take place at approximately regular intervals separated by the specified period. In Fixed-delay execution, each execution is scheduled relative to the actual execution of the previous. If an execution is delayed to any reason (such as garbage collection or other background activity), subsequent Executio NS would be delayed as. In the long run, the frequency of execution would generally be slightly lower than the reciprocal of the specified peri
    
OD (assuming the system clock underlying object.wait (long) is accurate). Fixed-delay execution is appropriate to recurring activities that require "smoothness." In other words, it's appropriate for activities where it's more important to keep the frequency accurate in theRun than in the long run. This is includes most animation tasks, such as blinking a cursor at regular. It also includes tasks wherein regular the activity is performed into response to human input, such as automatically Repeati
    
Ng a character as long as a key is held down.
Parameters:task-task to be scheduled.
Delay-delay in milliseconds before the task is executed.
Period-time in milliseconds between successive task executions.
Throws:illegalargumentexception-if delay is negative, or delay + system.currenttimemillis () is negative.
Illegalstateexception-if task is already scheduled or cancelled, timer was cancelled, or timer thread terminated. Description: This method performs the task after the specified delay, and performs the task at timed intervals.

Related Article

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.