Android Wear計時器開發(fā)
記得在2013年12月的時候,有系列文章是介紹怎么開發(fā)一個智能手表的App,讓用戶可以在足球比賽中記錄停表時間。隨著Android Wear的問世,在可穿戴設(shè)備中開發(fā)一款這樣的App確實是個很不錯的想法,但是按照目前對于Android Wear的架構(gòu)了解來說,似乎有些困難。所以本系列文章我們就重寫這個應(yīng)用,帶領(lǐng)大家進(jìn)入Android Wear的世界。
本文不會長篇大論地講解我們要開發(fā)的這款A(yù)pp的用途,因為我們在之前的系列文章已經(jīng)深入了解過了。這么說吧,這是一個計時類應(yīng)用,在比賽開始的時候開始執(zhí)行,在比賽的過程中可以暫停(停表),然后45分鐘過去后會有震動提醒,然后比賽進(jìn)行45分鐘后也會有提醒。
在開始之前,很有必要先看看我們?yōu)槭裁匆貙戇@個App而不是直接上代碼。智能手表使用的是一個修改版的Android1.6的系統(tǒng),所以它的架構(gòu)很像一個運行Android1.6的手機(jī),所以我們的App基于一個Activity,我們所有的工作都運行在這個Activity上。在開始學(xué)習(xí)智能手表開發(fā)之前,我們必須很清楚地知道,我們之前的設(shè)計在Android Wear上并不適用,盡管它也是支持Activity,但是在Android Wear上工作方式是不同的。在手機(jī)或者平板上,如果一個Activity從sleep狀態(tài)回到喚醒狀態(tài),Activity會被重新喚醒,但是在Wear上卻不是這樣。一段時間過去后Wear設(shè)備會進(jìn)入sleep,但是在設(shè)備喚醒后,處于sleep狀態(tài)的Activity卻不會再被喚醒了。
首先這個問題使我非常驚訝,我一直很想知道Activity有了這個限制后,還能開發(fā)實用的App嗎?后來才發(fā)現(xiàn)這個問題完全是多慮的,我漸漸地發(fā)現(xiàn),要開發(fā)一個實用的App也很簡單——我們只需要轉(zhuǎn)變我們的軟件設(shè)計模式,使它更符合Android Wear的體系結(jié)構(gòu),而不是當(dāng)做一個手機(jī)來看。
這里我們需要考慮的最基本的問題是,這個計時應(yīng)用程序需要基于一個一直運行的服務(wù)來記錄時間。但是基于長運行的服務(wù)不是一個好的方案,因為它會耗電。這里我們提到的記錄時間這個關(guān)鍵詞,也就是說,我們并不需要真的實現(xiàn)一個長運行的服務(wù),只要在用戶需要看的時候我們可以更新消息顯示就行。在大部分的時間里,其實用戶只需要了解大概過去了多長時間,只有在比賽暫停或者中場快結(jié)束的時候才需要顯示更詳細(xì)的信息。所以在大部分的時間里,我們只需要顯示精確到分鐘即可,然后在用戶需要的時候才精確到秒。
我們要實現(xiàn)這個方法的基本方法就是使用AlarmManager每分鐘觸發(fā)一次更新通知事件,去更新分鐘顯示。這個通知事件還包括顯示精確到秒的Activity,但是只有在用戶滑動屏幕的時候才會顯示整個通知。通過這種方式我們可以在必須顯示的時候才去更新消息,所以對大部分設(shè)備來說,每分鐘更新一次消息顯示比一直運行一個服務(wù)更加省電。
下圖顯示充分證明了這點,首先我們需要打開通知,這樣就可以得到精確到秒的顯示了。

然而,在有信息顯示或者設(shè)備休眠的時候,我們只需要顯示精確到分鐘就可以了。


有一件事情需要說明一下,就是這個App的名字已經(jīng)改變了。之前在在I'm Watch的版本上叫做“Footy Timer”,現(xiàn)在改為“Match Timer”。因為在使用語音啟動App的時候,Google的聲音識別對“Footy”這個詞很不敏感,我們用“ok Google,start Footy Timer”這個命令不能啟動應(yīng)用,而使用“ok Google,start Match Timer”就可以使用。
最后,很抱歉這篇文章沒有代碼,但是本系列文章會稍微有些變動。以前本人會在每篇文章末尾附上文章相關(guān)的代碼段,這個請放心,之后的文章還是會這樣的,因為這個是一個功能完善的App,而不是系列技術(shù)文章,所以在接下來的文章會包含一些代碼示例和注釋,在本系列文章完結(jié)的時候會附上整個項目的源碼。
Match Timer 可以在Google Play上找到:https://play.google.com/store/apps/details?id=com.stylingandroid.matchtimer
上面我們解釋了為什么要在Android Wear重寫這個計時器app(因為之前已經(jīng)在“I'm Watch”里面開發(fā)過了),下面我們就來看看代碼。
我們以這個app的一個核心類開始,這個類負(fù)責(zé)控制計時器的狀態(tài)。這個類包含了4個long類型的變量:第一個代表計時器開始的時間;第二個代表計時器停止的時間(在運行中的話,它就是0);第三個代表計時器停表的時間(如果當(dāng)前沒有停表,那它也是0),第四個代表總共停表的時長。通過這四個變量我們就可以維持計時器的狀態(tài)了,還可以通過計算得到我們需要展示的其他信息。這個類的基本功能就是都是為了操作這些變量,即維持計時器的這些狀態(tài)。
public final class MatchTimer {
.
.
.
public static final int MINUTE_MILLIS = 60000;
private long start;
private long currentStoppage;
private long totalStoppages;
private long end;
.
.
.
public long getElapsed() {
if (isRunning()) {
return System.currentTimeMillis() - start;
}
if (end > 0) {
return end - start;
}
return 0;
}
public boolean isRunning() {
return start > 0 && end == 0;
}
public boolean isPaused() {
return currentStoppage > 0;
}
public int getElapsedMinutes() {
return (int) ((System.currentTimeMillis() - start) / MINUTE_MILLIS);
}
public long getTotalStoppages() {
long now = System.currentTimeMillis();
if (isPaused()) {
return totalStoppages + (now - currentStoppage);
}
return totalStoppages;
}
public long getPlayed() {
return getElapsed() - getTotalStoppages();
}
public long getStartTime() {
return start;
}
.
.
.
}
這些都是基本的java代碼,就不費時間講了。下面的函數(shù)更高級一些,可以操作計時器的狀態(tài)。
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()方法我們還沒有見到,這是在類的最后寫的,這個函數(shù)才值得的我們講講。
前一篇文章我們討論了關(guān)于喚醒機(jī)制的問題,我們不需要去維持一個長連接或者后臺服務(wù),只需要維持這幾個計時器的狀態(tài)就可以了。我們使用SharedPreference來實現(xiàn):
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;
}
}
.
.
.
}
我們需要的就是newInstance()方法從SharedPreference中構(gòu)造一個MatchTimer實例,我們還需要save()方法,可以幫我們把當(dāng)前的計時器狀態(tài)保存到SharedPreference中。
最后我們要說明的是,如果某一部分持有MatchTimer對象的引用,但是其他對象已經(jīng)改變了計時器的狀態(tài),就可能會發(fā)生異常(見下一篇文章)。所以我們還需要提供一些方法去注冊和注銷MatchTImer的實例,在Sharedpreference的值改變時去接收計時器狀態(tài)的變化。
現(xiàn)在我們已經(jīng)定義了一個基本的計時器了,下一篇文章我們會介紹怎么保持計時器的狀態(tài)以及在需要的時候去喚醒這些狀態(tài)。
Match Timer 可以在Google Play上下載:Match Timer.
在本系列前幾篇文章中,我們介紹了Android Wear計時器app,對設(shè)計思路和app的結(jié)構(gòu)進(jìn)行了分析。本文將講解如何定時喚醒程序提醒用戶。
對于為什么不用后臺服務(wù)的方式一直運行,我們已經(jīng)進(jìn)行了解釋——這種方式非常耗電。因此,我們必須要有一個定時喚醒機(jī)制。我們可以使用AlarmManager來實現(xiàn)這個機(jī)制,定時執(zhí)行一個Intent,然后通知BroadcastReceiver。之所以選擇BroadcastReceiver而不用IntentService,是因為我們要運行的任務(wù)是輕量級的而且生命周期非常短暫。使用BroadcastReceiver可以避免每次執(zhí)行任務(wù)的時候都經(jīng)歷Service的整個生命周期。因此,對于我們這種輕量級的任務(wù)來說非常合適——我們執(zhí)行的任務(wù)都在毫秒級。
BroadcastReceiver的核心在于onReceiver方法,我們需要在這里安排各種事件響應(yīng)。
public class MatchTimerReceiver extends BroadcastReceiver {
public static final int MINUTE_MILLIS = 60000;
private static final long DURATION = 45 * MINUTE_MILLIS;
private static final Intent UPDATE_INTENT = new Intent(ACTION_UPDATE);
private static final Intent ELAPSED_ALARM = new Intent(ACTION_ELAPSED_ALARM);
private static final Intent FULL_TIME_ALARM = new Intent(ACTION_FULL_TIME_ALARM);
private static final int REQUEST_UPDATE = 1;
private static final int REQUEST_ELAPSED = 2;
private static final int REQUEST_FULL_TIME = 3;
public static void setUpdate(Context context) {
context.sendBroadcast(UPDATE_INTENT);
}
.
.
.
private void reset(MatchTimer timer) {
timer.reset();
}
private void resume(Context context, MatchTimer timer) {
timer.resume();
long playedEnd = timer.getStartTime() + timer.getTotalStoppages() + DURATION;
if (playedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM, playedEnd);
}
}
private void pause(Context context, MatchTimer timer) {
timer.pause();
cancelAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM);
long elapsedEnd = timer.getStartTime() + DURATION;
if (!isAlarmSet(context, REQUEST_ELAPSED, ELAPSED_ALARM) && elapsedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_ELAPSED, ELAPSED_ALARM, elapsedEnd);
}
}
private void stop(Context context, MatchTimer timer) {
timer.stop();
cancelAlarm(context, REQUEST_UPDATE, UPDATE_INTENT);
cancelAlarm(context, REQUEST_ELAPSED, ELAPSED_ALARM);
cancelAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM);
}
private void start(Context context, MatchTimer timer) {
timer.start();
long elapsedEnd = timer.getStartTime() + DURATION;
setRepeatingAlarm(context, REQUEST_UPDATE, UPDATE_INTENT);
if (timer.getTotalStoppages() > 0 && !timer.isPaused()) {
long playedEnd = timer.getStartTime() + timer.getTotalStoppages() + DURATION;
if (playedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM, playedEnd);
}
if (elapsedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_ELAPSED, ELAPSED_ALARM, elapsedEnd);
}
} else {
if (elapsedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM, elapsedEnd);
}
}
}
.
.
.
}
代碼還是非常直觀易于理解的。首先實例化一個MatchTimer對象(從SharedPreference中讀取數(shù)據(jù)),然后分別傳給對應(yīng)的事件處理Handler。之后等待動作發(fā)生,最后更新Notification。
這里會處理8個事件動作,其中5個負(fù)責(zé)控制計時器的狀態(tài)(START、STOP、PAUSE、RESUME、RESET);一個負(fù)責(zé)更新Notification,剩下兩個負(fù)責(zé)到45分鐘喚醒后震動提示。
我們先從這幾個控制狀態(tài)開始:
public class MatchTimerReceiver extends BroadcastReceiver {
public static final int MINUTE_MILLIS = 60000;
private static final long DURATION = 45 * MINUTE_MILLIS;
private static final Intent UPDATE_INTENT = new Intent(ACTION_UPDATE);
private static final Intent ELAPSED_ALARM = new Intent(ACTION_ELAPSED_ALARM);
private static final Intent FULL_TIME_ALARM = new Intent(ACTION_FULL_TIME_ALARM);
private static final int REQUEST_UPDATE = 1;
private static final int REQUEST_ELAPSED = 2;
private static final int REQUEST_FULL_TIME = 3;
public static void setUpdate(Context context) {
context.sendBroadcast(UPDATE_INTENT);
}
.
.
.
private void reset(MatchTimer timer) {
timer.reset();
}
private void resume(Context context, MatchTimer timer) {
timer.resume();
long playedEnd = timer.getStartTime() + timer.getTotalStoppages() + DURATION;
if (playedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM, playedEnd);
}
}
private void pause(Context context, MatchTimer timer) {
timer.pause();
cancelAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM);
long elapsedEnd = timer.getStartTime() + DURATION;
if (!isAlarmSet(context, REQUEST_ELAPSED, ELAPSED_ALARM) && elapsedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_ELAPSED, ELAPSED_ALARM, elapsedEnd);
}
}
private void stop(Context context, MatchTimer timer) {
timer.stop();
cancelAlarm(context, REQUEST_UPDATE, UPDATE_INTENT);
cancelAlarm(context, REQUEST_ELAPSED, ELAPSED_ALARM);
cancelAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM);
}
private void start(Context context, MatchTimer timer) {
timer.start();
long elapsedEnd = timer.getStartTime() + DURATION;
setRepeatingAlarm(context, REQUEST_UPDATE, UPDATE_INTENT);
if (timer.getTotalStoppages() > 0 && !timer.isPaused()) {
long playedEnd = timer.getStartTime() + timer.getTotalStoppages() + DURATION;
if (playedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM, playedEnd);
}
if (elapsedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_ELAPSED, ELAPSED_ALARM, elapsedEnd);
}
} else {
if (elapsedEnd > System.currentTimeMillis()) {
setAlarm(context, REQUEST_FULL_TIME, FULL_TIME_ALARM, elapsedEnd);
}
}
}
.
.
.
}
這些方法主要有兩個功能:首先設(shè)置MatchTimer的狀態(tài),然后設(shè)置時間提醒的鬧鈴,改變參數(shù)就可以播放鬧鈴。這個功能還可以封裝成一個工具方法,叫setUpdate()。這樣外部也可以觸發(fā)計時器的更新。
我們使用標(biāo)準(zhǔn)AlarmManager的方法來設(shè)置鬧鈴:
public class MatchTimerReceiver extends BroadcastReceiver {
.
.
.
public static final int MINUTE_MILLIS = 60000;
.
.
.
private void setRepeatingAlarm(Context context, int requestCode, Intent intent) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), MINUTE_MILLIS, pendingIntent);
}
private boolean isAlarmSet(Context context, int requestCode, Intent intent) {
return PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_NO_CREATE) != null;
}
private void setAlarm(Context context, int requestCode, Intent intent, long time) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
private void cancelAlarm(Context context, int requestCode, Intent intent) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
.
.
.
}
這里值得討論的是setRepeatingAlarm()這個方法。因為在Wear在實現(xiàn)方式上有點不一樣。我們會在Start事件中每秒鐘觸發(fā)一次鬧鈴更新Notification動作,所以這里需要記錄具體已經(jīng)過去了多少分鐘。正常來說我們會每隔60秒觸發(fā)一次這個動作,但是在Wear上不能這么做。原因是——當(dāng)設(shè)備在喚醒著的時候可以這樣做,但是如果設(shè)備進(jìn)入睡眠狀態(tài)就需要重新計算下一分鐘的邊界值。這就需要異步更新部件,然后設(shè)備只需要每分鐘喚醒一次。一分鐘結(jié)束后在計時器需要更新狀態(tài)的時候觸發(fā)操作。
對于我們的計時器應(yīng)用來說,顯示的分鐘數(shù)會比實際時間少1分鐘。但是顯示分鐘并不要求非常實時(但顯示秒數(shù)時需要非常精確),所以我們可以這樣操作:
完整的alarm Handler是這樣使用振動服務(wù)的:
public class MatchTimerReceiver extends BroadcastReceiver {
.
.
.
private static final long[] ELAPSED_PATTERN = {0, 500, 250, 500, 250, 500};
private static final long[] FULL_TIME_PATTERN = {0, 1000, 500, 1000, 500, 1000};
private void elapsedAlarm(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(ELAPSED_PATTERN, -1);
}
private void fullTimeAlarm(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(FULL_TIME_PATTERN, -1);
}
.
.
.
}
最后,我們通過這個方法來構(gòu)造Notification然后呈現(xiàn)給用戶:
public class MatchTimerReceiver extends BroadcastReceiver {
public static final int NOTIFICATION_ID = 1;
.
.
.
private void updateNotification(Context context, MatchTimer timer) {
NotificationBuilder builder = new NotificationBuilder(context, timer);
Notification notification = builder.buildNotification();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
Notification是Wear計時器的一個重要的部分,這里還需要一個自定義類來構(gòu)造這些Notification通知。下一篇文章我們會講如何在計時器app中使用Notification。
Match Timer可以在Google Play上下載:Match Timer。
相關(guān)文章
最近較流行的效果 Android自定義View實現(xiàn)傾斜列表/圖片
最近較流行的效果,這篇文章主要介紹了Android自定義View實現(xiàn)傾斜列表/圖片的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06
Android WebView上實現(xiàn)JavaScript與Java交互
這篇文章主要介紹了Android WebView上實現(xiàn)JavaScript與Java交互 的相關(guān)資料,需要的朋友可以參考下2016-03-03
從零開始使用gradle配置即可執(zhí)行的Hook庫詳解
這篇文章主要為大家介紹了從零開始使用gradle配置即可執(zhí)行的Hook庫詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時出現(xiàn)空指針(NullPointerException)問題的解決
這篇文章主要介紹了Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時出現(xiàn)空指針(NullPointerException)問題的解決方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Android 監(jiān)聽軟鍵盤狀態(tài)的實例詳解
這篇文章主要介紹了Android 監(jiān)聽軟鍵盤狀態(tài)的實例詳解的相關(guān)資料,希望通過本文能掌握這樣的知識,需要的朋友可以參考下2017-09-09

