Reliability of the postDelay method for Android

I am using the android.os.Handler postDelayed method to turn the relay on and off. There are 2 runnable - one for sending the ON command, and another for sending the OFF command. The relay must be saved for fixed ms and turned off again for fixed ms.

I mentioned this old Android post http://android-developers.blogspot.in/2007/11/stitch-in-time.html for sending repeated commands to the relay

The start command must be sent simultaneously from different devices - each of them is connected to another relay. Thus, the effect must be such that all relays are switched on simultaneously.

I guarantee that a command will be sent simultaneously from each device using GPS time synchronization on all devices. After all the devices are synchronized by GPS time, I send the ON command at the beginning of the second for the first time, and then add fixed delays to the postDelay methods in both executable files.

I still notice that the relays do not turn on at the same time for the first time. I notice a delay of up to 1 second.

I need to know how reliable is the postDelayed method? Can I trust him with accuracy of up to 5 ms? Is there a more reliable way to send a repeated command with a fixed delay?

Here is a piece of code

public class RelayAsyncTask extends AsyncTask<Void, Integer, Boolean>
    {
        private Activity context = null;
        private Handler handler = null;
        private Runnable offRunnable,onRunnable;


        @Override
        protected void onPreExecute() 
        {
            handler = new Handler();
            onRunnable = new Runnable() {
                               
                                @Override
                                 public void run() {
                                               new RelayTurnOnAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
                                               publishProgress(1);  // used to display on UI that a command has been sent
                                               handler.postDelayed(offRunnable, 500);
                                      
                                }
                        };

            offRunnable = new Runnable() {
                               
                                @Override
                                public void run() {
                                               new RelayTurnOffAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
                                               publishProgress(2);  // used to display on UI that a command has been sent
                                               handler.postDelayed(onRunnable, 1000);
                                                
                                       
                                }
                         };
            super.onPreExecute();
        }


        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                // Code to calculate diff when first ON command should be sent
                .
                .
                handler.postDelayed(onRunnable, diff);

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            switch (values[0]) {
            case 0:
            {
                break;
            }
            case 1:
            {   //code to update UI
                .
                .
                break;
            }
            case 2:
            {   //code to update UI
                .               .
                .
                break;
            }
            default:
                break;
            }

        }


        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
        }
    }   

I also notice a delay of up to 10 ms when RelayTurnOnAsyncTask (). executeOnExecutor (AsyncTask.THREAD_POOL_EXECUTOR, null) is called.

+4
1

asynctask.

, , doInBackground, postDelayed, , publishProgress, .

0

All Articles