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.