Android applications sometimes need to perform tasks automatically at fixed time intervals.
Examples include:
- Clock applications
- Countdown timers
- Session tracking
- Analytics updates
- Dashboard refresh systems
Android provides a system broadcast called:
Intent.ACTION_TIME_TICK
This broadcast triggers every minute when the system clock changes minute values.
In this tutorial, we will learn how to update the user interface every minute using a BroadcastReceiver in Android.
What Is ACTION_TIME_TICK?
ACTION_TIME_TICK is a system broadcast sent by Android every minute.
It allows applications to:
- Refresh UI automatically
- Update timers
- Track minute changes
- Execute lightweight periodic tasks
Important Limitation
The ACTION_TIME_TICK broadcast:
- Cannot be registered in AndroidManifest.xml
- Must be registered dynamically at runtime
What We Will Build
In this Android example:
- Application listens for minute changes
- Counter increases every minute
- UI updates automatically
- BroadcastReceiver registers dynamically
Step 1 — Create activity_main.xml
Create the layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="60sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 2 — Implement MainActivity.java
Open:
MainActivity.java
Add the following code:
package com.example.minuteupdater;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity
extends AppCompatActivity {
private TextView textViewCounter;
private BroadcastReceiver minuteReceiver;
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewCounter =
findViewById(R.id.textViewCounter);
}
private void startMinuteUpdater() {
IntentFilter intentFilter =
new IntentFilter(
Intent.ACTION_TIME_TICK
);
minuteReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(
Context context,
Intent intent
) {
counter++;
textViewCounter.setText(
String.valueOf(counter)
);
}
};
registerReceiver(
minuteReceiver,
intentFilter
);
}
@Override
protected void onResume() {
super.onResume();
startMinuteUpdater();
}
@Override
protected void onPause() {
super.onPause();
if (minuteReceiver != null) {
unregisterReceiver(minuteReceiver);
}
}
}
How This Implementation Works
The workflow is:
- Activity starts
- BroadcastReceiver registers dynamically
- Android sends ACTION_TIME_TICK every minute
- Receiver captures broadcast
- Counter increments
- UI updates automatically
Why Dynamic Registration Is Required
Android does not allow:
ACTION_TIME_TICK
inside:
AndroidManifest.xml
for battery optimization reasons.
Therefore, developers must register it dynamically using:
registerReceiver()
Why unregisterReceiver() Is Important
Failing to unregister BroadcastReceivers can cause:
- Memory leaks
- Application crashes
- Lifecycle issues
Always unregister receivers inside:
onPause()
or
onDestroy()
Common Mistakes Developers Make
1. Registering Receiver Multiple Times
Repeated registrations can cause duplicate callbacks.
2. Forgetting unregisterReceiver()
This may cause:
- IllegalArgumentException
- Memory leaks
3. Performing Heavy Work Inside onReceive()
BroadcastReceivers should only perform lightweight operations.
Modern Android Recommendations
Modern Android applications should prefer:
- WorkManager
- Lifecycle-aware components
- ViewModel
- Coroutines
- Jetpack Compose state handling
ACTION_TIME_TICK vs Timer
| ACTION_TIME_TICK | CountDownTimer |
|---|---|
| System-level minute updates | Custom timer implementation |
| Battery optimized | Runs continuously |
| Every minute only | Flexible intervals |
FAQ
Can ACTION_TIME_TICK run in background?
It works only while the application process is active.
Can ACTION_TIME_TICK be declared in AndroidManifest.xml?
No. Android restricts manifest registration for this broadcast.
What is the modern alternative?
For periodic background work, WorkManager is the recommended modern solution.
Conclusion
BroadcastReceivers provide an efficient way to react to system events in Android.
Using:
Intent.ACTION_TIME_TICK
developers can update UI automatically every minute with minimal overhead.
Modern Android applications should combine lifecycle-aware architecture with optimized background processing solutions for scalable performance.
About the Author
Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and developer tooling products.
CodeChain Dev — Build Modern Products. Solve Real Problems.
No comments:
Post a Comment