Android Wear / SharedPreferences

Match Timer – Part 2

In the previous article we looked at some of the decisions behind re-writing the Footy Timer app (which was previously developed for the I’m Watch) in order to implement it on Android Wear. In this article we’ll begin looking at the code.

matchtimerWe’ll start with the class at the heart of the app which is responsible for holding the current timer state. This is essentially a class containing four long values: The first contains the time the timer started; the second contains the time the timer was stopped (or 0 if the timer is currently running); the third contains the timer the game was paused (or 0 if the time is not currently paused); and the fourth contains the total time that timer has been paused. From these four values we can maintain the timer state, and also keep enough information to calculate all of the information that we need to display. The basic function of this class is to maintain these values. It provides a set of methods which will retrieve the timer state:

This is all pretty basic Java, there’s nothing Android specific so I won’t bother explaining it. The next set of methods augment these and permit the timer state to be changed:

Once again, this is basic Java, but there’s a save() method which we’ve yet to see, and this gives a clue to the final part of this class, and this is where we have something that deserves a little explanation.

In the previous article we discussed how we only want to wake very occasionally, and we don’t want to maintain any long running services or processes, so we need some method of maintaining the timer state. We’ll use SharedPreferences to do this:

What we have here is a newInstance() method which will obtain a MatchTimer instance from SharedPreferences (i.e. it will obtain an instance from the persisted state). We also have the save() method, which we saw earlier, which will persist the current timer state to SharedPreferences.

The final aspect of this is that there may be occasions where one of the components is holding a reference to a MatchTimer object which needs to be updated because another component has changed the state (we’ll see this later in the series). So we provide a couple of methods which will register and unregister the MatchTimer instance to receive automatic updates when the underlying SharedPreferences values are changed.

So we have the basic timer now defined. In the next article we’ll look at how we can maintain the timer state and wake up when we need to.

Match Timer is available on Google Play.


Get it on Google Play

© 2014, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Match Timer – Part 2 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

2 Comments

  1. I have a question for you. You are using multiple instances of MatchTimer and all the instances are updated using SharedPreferencesChangeListener whenever an update occurs. Instead of it, wouldn’t it be better if you use singleton instance of MatchTimer everywhere? Is there a specific intention why you did this this way?

Leave a Reply

Your email address will not be published. Required fields are marked *