SoFunction
Updated on 2025-04-10

Android implements timing task function

This article shares the specific code for Android to implement timing task function for your reference. The specific content is as follows

1. Use the sleep(long) method of Handle and thread

(1) Define a Handler class to handle the received Message.

Handler handler = new Handler() {  
    public void handleMessage(Message msg) {  
        // Things to do        (msg);  
    }  
}; 

(2) Create a new thread class that implements the Runnable interface, as follows:

public class MyThread implements Runnable {  
    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        while (true) {  
            try {  
                (10000);// Thread pauses for 10 seconds, unit milliseconds                Message message = new Message();  
                 = 1;  
                (message);// Send a message            } catch (InterruptedException e) {  
                // TODO Auto-generated catch block  
                ();  
            }  
        }  
    }  
}

(3) Add the following statement where the thread needs to be started:

new Thread(new MyThread()).start();  

2. Use Handler's postDelayed(Runnable, long) method

(1) Define a Handler class

Handler handler=new Handler();  
 
Runnable runnable=new Runnable() {  
    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        //Things to do        (this, 2000);  
    }  
}; 

(2) Start

(runnable, 2000);

3. Methods for combining Handler with timer and TimerTask

(1) Define timer, timer task and Handler handle

private final Timer timer = new Timer();  
private TimerTask task;  
Handler handler = new Handler() {  
    @Override  
    public void handleMessage(Message msg) {  
        // TODO Auto-generated method stub  
        // Things to do        (msg);  
    }  
}; 

(2) Initialize the timer task

task = new TimerTask() {  
    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        Message message = new Message();  
         = 1;  
        (message);  
    }  
}; 

(3) Start and close the timer

(task, 2000, 3000);   
(); 

4. Use AlarmManger to achieve long-term and accurate timing tasks

(1) Service category:

public class HorizonService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ("TAG", "Print time: " + new Date().
                        toString());
            }
        }).start();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int five = 5000; // This is 5s        long triggerAtTime = () + five;
        Intent i = new Intent(this, );
        PendingIntent pi = (this, 0, i, 0);
        (AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return (intent, flags, startId);
    }
}

(2) Broadcast receiver

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, );
        (i);
    }
}

(3) Start the timing task:

Intent intent = new Intent(this,);
startService(intent);

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.