Skip to content

Instantly share code, notes, and snippets.

@klaaspieter
Created February 11, 2014 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaaspieter/8936096 to your computer and use it in GitHub Desktop.
Save klaaspieter/8936096 to your computer and use it in GitHub Desktop.

Every second refresh the hotspot status

For the Karma Android (and iOS app) we need to update the hotspot status every second. We'll consider the exact implementation a detail suffice to say that every second some callback needs to be triggered that performs the necesary update. I'm writing this gist to try and explain what I'm trying to do and to get feedback on my approach from experienced developers.

My first attempt

  1. The main activity has an instance of a HotspotStatusUpdater class (initialize in onCreate)
  2. In onResume I call hotspotStatusUpdater.startUpdatingHotspotStatus(this); (this is a callback that implements the HotspotStatusCallback interface)
  3. In onPause I call hotspotStatusUpdater.stopUpdating();

Abstractly the hotspot status updater was implemented like this:

  1. When it's initialized create the client that is responsible for actually update the hotspot status. The updater is simply a host for calling this client every second.
  2. Create a ScheduledExecutorService using Executors.newScheduledThreadPool(1);
  3. When startUpdatingHotspotStatus is called use scheduledExecutor.scheduleAtFixedRate to schedule a fetch every second
  4. When the client calls back with the update hotspot status callback to the activity

Looking back after only a little over a week of Android development I realize this is a flawed approach. It is flawed because it does not take the Activity lifecycle into account. More concretely the client reports a new hotspot status on a different thread. When the client finishes it posts it update on it's own thread meaning you're calling back to your UI on a background thread.

This I initially fixed using:

Handler mainHandler = new Handler(updater.context.getMainLooper());
mainHandler.post(new Runnable() {
    @Override
    public void run() {
        callback.hotspotStatusDidUpdate(hotspotStatus);
    }
});

At first I thought this was just Java being Java. I dislike this because it puts me as a developer very close to actual threading which is something you should keep me (and most other developers) very far away from.

The second attempt

Even after this 'fix' I kept running into random crashes with configuration changes (rotated the device). I kept adding conditionals until I realized that if I look at this code 1 week later I have no clue which edge case that conditional is fixing. I started looking for other approaches that are hopefully more idomatic Android. This is what I have come up with right now. Feel free to give me feedback.

  1. The MainActivity hosts a UI-less HotspotUpdaterFragment
  2. The HotspotUpdaterActivity uses a AsyncTaskLoader subclass called HotspotStatusLoader to 'load' the hotspot status
  3. The AsyncTaskLoader uses something (not sure what yet) to trigger onContentChanged on the Loader every second

My assumption is that because I'm using a loader, which are designed to work with activities and fragments, I will have to deal less with the activity lifecycle. If you have any feedback to this approach please let me know! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment