Android Wear计时器开发(2)
这些都是基本的java代码,就不费时间讲了。下面的函数更高级一些,可以操作计时器的状态。 public final class MatchTimer { . . . public void start() { if (end 0) { st
这些都是基本的java代码,就不费时间讲了。下面的函数更高级一些,可以操作计时器的状态。
public final class MatchTimer { . . . public void start() { if (end > 0) { start = System.currentTimeMillis() - (end - start); end = 0; } else { start = System.currentTimeMillis(); } save(); } public void stop() { if (isPaused()) { resume(); } end = System.currentTimeMillis(); save(); } public void pause() { currentStoppage = System.currentTimeMillis(); save(); } public void resume() { totalStoppages += System.currentTimeMillis() - currentStoppage; currentStoppage = 0L; save(); } public void reset() { resetWithoutSave(); save(); } private void resetWithoutSave() { start = 0L; currentStoppage = 0L; totalStoppages = 0L; end = 0L; } }
这些还是基本的Java代码,也可以不用讲了。只有save()方法我们还没有见到,这是在类的最后写的,这个函数才值得的我们讲讲。
前一篇文章我们讨论了关于唤醒机制的问题,我们不需要去维持一个长连接或者后台服务,只需要维持这几个计时器的状态就可以了。我们使用SharedPreference来实现:
public final class MatchTimer implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String KEY_START = "com.stylingandroid.matchtimer.KEY_START"; private static final String KEY_CURRENT_STOPPAGE = "com.stylingandroid.matchtimer.KEY_CURRENT_STOPPAGE"; private static final String KEY_TOTAL_STOPPAGES = "com.stylingandroid.matchtimer.KEY_TOTAL_STOPPAGES"; private static final String KEY_END = "com.stylingandroid.matchtimer.KEY_END"; private static final String PREFERENCES = "MatchTimer"; private final SharedPreferences preferences; public static MatchTimer newInstance(Context context) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE); long start = preferences.getLong(KEY_START, 0); long currentStoppage = preferences.getLong(KEY_CURRENT_STOPPAGE, 0); long totalStoppages = preferences.getLong(KEY_TOTAL_STOPPAGES, 0); long end = preferences.getLong(KEY_END, 0); return new MatchTimer(preferences, start, currentStoppage, totalStoppages, end); } private MatchTimer(SharedPreferences preferences, long start, long currentStoppage, long totalStoppages, long end) { this.preferences = preferences; this.start = start; this.currentStoppage = currentStoppage; this.totalStoppages = totalStoppages; this.end = end; } public void save() { preferences.edit() .putLong(KEY_START, start) .putLong(KEY_CURRENT_STOPPAGE, currentStoppage) .putLong(KEY_TOTAL_STOPPAGES, totalStoppages) .putLong(KEY_END, end) .apply(); } public void registerForUpdates() { preferences.registerOnSharedPreferenceChangeListener(this); } public void unregisterForUpdates() { preferences.unregisterOnSharedPreferenceChangeListener(this); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { long value = sharedPreferences.getLong(key, 0L); if (key.equals(KEY_START)) { start = value; } else if (key.equals(KEY_END)) { end = value; } else if (key.equals(KEY_CURRENT_STOPPAGE)) { currentStoppage = value; } else if (key.equals(KEY_TOTAL_STOPPAGES)) { totalStoppages = value; } } . . . }
- 上一篇:Android自定义ActionBar实例
- 下一篇:没有了
收藏文章
精彩图集
精彩文章