Search code examples
androidmultithreadingservicetimercountdown

Parallel Countdown Timers


I have a doubt about how will be the best way to make work with more than one Countdown Timer at the same time. I'm already doing it (on the UI Thread) and I'm updating many components (Textview, rv with data, imageviews). It works, but I'm noticing that the UI is getting a little laggy when I want to switch the "alarm" thatI'im showing.

I also want to make this alarms run on the background with a notification showing remaining time, which I think I could never do with this UI Thread thing I've made.

What would be the correct way of doing this?


Solution

  • From any service or from any intentservice or from asynctask you can have timer thread ( i.e. count-down timer in your case ) like :

    public void Timer()
    {
        new Thread(new Runnable()
        {
            public void run()
            {
                while (IsOnGoing)
                {
                    try
                    {
                        TimeUnit.SECONDS.sleep(1);
                        seconds++;
    
                        int hour = seconds/3600;
                        int remaining = seconds%3600;
    
                        int minutes = remaining/60;
                        int seconds = remaining%60;
    
                        String hourString = (hour<10 ? "0" : "")+hour;
                        String minutesString = (minutes<10 ? "0" : "")+minutes;
                        String secondsString = (seconds<10 ? "0" : "")+seconds;
    
                        String InCallDuration = hourString + " : " + minutesString + " : " + secondsString;
    
                        Intent intent = new Intent("ticks");
                        intent.setPackage(getPackageName());
                        intent.putExtra("InCallDuration", InCallDuration);
                        getApplicationContext().sendBroadcast(intent);
    
                        Log.d("InCallService :", "InCallDuration"+ InCallDuration+".. \n");
                    }
                    catch (InterruptedException e)
                    {
                        Log.d("CallStateService :", "InterruptedException.. \n");
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    
    }
    

    If you notice line from above code ( mentioned below ) :

    getApplicationContext().sendBroadcast(intent);
    

    Which is sending local broadcast to the same application. ( i.e. sending broadcast from our app to our app only. )

    Register it in any activity like :

    IntentFilter filterTicks = new IntentFilter("ticks");
    registerReceiver(secondsBroadcastReceiver, filterTicks);
    

    Un-register in the same activity once tasks are finished / onDestroy / OnPause :

    unregisterReceiver(secondsBroadcastReceiver);
    

    How to use :

    You have noticed broadcastreceiver in above code like :

    private BroadcastReceiver secondsBroadcastReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String InCallDuration = intent.getStringExtra("InCallDuration");
            Log.d("CallActivity :", "Received InCallDuration:" + InCallDuration + ".. \n");
            CallTime.setText(InCallDuration);
        }
    };
    
    1. From which you can set / change the UI textview text as many times it ticks in background.
    2. We just need to send broadcast after every tick
    3. Its appropriate receiver receives it and updates UI ( we need to set it )
    4. It sends broadcasts and receives as long as it remains there.
    5. You Have To Only Replace The Timer Method as Your Ticks Will reduce, Now i left it for you to program rest.