I want to make repeated API requests every few seconds in my Android app. My current function fetches a base64 encoded image from a server and displays it in an ImageView. I need this to happen automatically at regular intervals to keep the image updated.
workManager’s probably overkill, but it beats everything else when ur app gets backgrounded. it’ll keep scheduling work even after android kills ur process. just set up a PeriodicWorkRequest for the image fetching - it survives process death and battery optimization. timers and handlers get destroyed when the system needs memory.
I’ve done similar image streaming and Timer with TimerTask works great. Just set it up in onCreate and schedule fetchImageData to run at regular intervals.
Timer imageTimer = new Timer();
imageTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(() -> fetchImageData());
}
}, 0, 3000); // 3 second intervals
You need runOnUiThread because TimerTask runs in the background but you’re updating UI stuff. Don’t forget to cancel the timer in onDestroy or you’ll get memory leaks. I make the timer a class variable so I can clean it up when the activity dies. Works solid in production - haven’t had issues with this approach.
Use ScheduledExecutorService. It’s got better control than Timer and cleaner lifecycle management than Handler approaches. I’ve used this for real-time data polling in several production apps.
private ScheduledExecutorService scheduler;
private ScheduledFuture<?> scheduledTask;
// In onCreate or wherever you start
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduledTask = scheduler.scheduleAtFixedRate(() -> {
runOnUiThread(() -> fetchImageData());
}, 0, 3, TimeUnit.SECONDS);
Cleanup’s easy in onDestroy. Cancel the task first, then shutdown the executor. This stops background tasks from running after your activity dies.
if (scheduledTask != null) scheduledTask.cancel(true);
if (scheduler != null) scheduler.shutdown();
Watch out for one thing with your current setup - cache the last successful image. Network hiccups happen all the time and users hate when images vanish during connectivity issues. Keep showing the previous image until you grab a new one.
Android solutions work but they’re old school. Why deal with all that complexity when you can offload the heavy work?
I’ve done this with streaming dashboards. Constantly polling from mobile kills battery and hammers your servers. You’re also fighting network drops, backgrounding, and memory issues.
Better move: Set up Latenode to handle image fetching and processing server-side. Build a workflow that grabs images every few seconds, processes them, and pushes updates to your app via webhooks or WebSockets.
Your Android app gets way simpler - just listen for updates instead of polling. No timers, handlers, or background thread headaches. Image processing happens reliably in the cloud no matter what your app’s doing.
Built something like this for a monitoring dashboard with live camera feeds. Moving polling logic to Latenode cut mobile battery usage 60% and made everything more reliable. The app just displays what gets pushed.
Latenode handles scheduling, retries, and errors automatically. Your fetchImageData becomes a simple webhook receiver instead of active poller.
Just call imageSubscription.dispose() in onDestroy. Done - no memory leaks.
RxJava handles threading way better than doing it manually. Built-in error handling with retry operators when network calls bomb. Easy to add throttling or debouncing if users mess around while images update.
I’ve used this for live dashboards in multiple apps. Way more solid than basic Timer stuff and doesn’t break during config changes if you manage the lifecycle right.