2017-08-16 77 views
1

我們的應用程序中有一個時鐘小部件。 小部件需要每分鐘更新一次以顯示正確的時間。Android O:我們可以製作一個時鐘小部件嗎?

在Android的O,則建議使用的jobscheduler後臺更新。 不幸的是,有一些限制。

  1. JobService的定期更新無法在少於15分鐘的間隔內調用。
  2. JobService.onStartJob()是不可預知的。我們可能會錯過更新分鐘數的準確時刻(第59秒)。

以前我們曾經使用Handler.postDelayed()來運行後臺服務來更新小部件中的時間。 在O後臺服務可以由系統終止。

你會如何建議實施的AndroidØ時鐘小工具?

現在這甚至可能嗎?

enter image description here

+1

任何想法,爲什麼我的問題是downvoted? – Pavel

+0

嘗試JobIntentService –

+2

這是一個非常重要的問題。它不應該downvoted –

回答

2

使用「TextClock」窗口小部件的控制,它是由遠程視窗功能的支持,它通過它的自我更新的每一分鐘,沒有必要使用那些花哨的jobscheduler工作。

像這樣的事情會做的伎倆:

<LinearLayout 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextClock 
     android:id="@+id/dateText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:format12Hour="@string/date_text_format_12h" 
     android:format24Hour="@string/date_text_format_24h" 
     android:gravity="bottom|center_horizontal" 
     android:textColor="@android:color/black" 
     android:textSize="@dimen/date_text_size_default"/> 

    <TextClock 
     android:id="@+id/timeText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:format12Hour="@string/time_text_format_12h" 
     android:format24Hour="@string/time_text_format_24h" 
     android:gravity="top|center_horizontal" 
     android:textColor="@android:color/black" 
     android:textSize="@dimen/time_text_size_default" 
     android:textStyle="bold"/> 

</LinearLayout> 
+1

我並不期望解決方案如此簡單。幾周前我們甚至聯繫了Google開發者支持。他們仍然沒有提供任何答案。非常感謝! – Pavel

+1

TextClock小部件僅適用於啓動API 17. – Pavel

+0

此解決方案不適用於模擬時鐘。有誰知道模擬時鐘是否可以在Android Oreo中創建? –

0

這是一個小部件;它不需要一直更新 - 只有當它可見時纔會更新。爲什麼你想在你的小部件沒有運行的時候浪費電池來跟蹤當前時間?

當小部件變爲可見,查找當前時間和繪製的時候了。另外,計算下一分鐘的頂部時間,並將延遲消息或Runnable發佈到將在此刻觸發的處理程序。當該消息/ Runnable執行時,更新您的時間顯示並在下一分鐘的頂部重新發布消息/ Runnable。

你也應該註冊,噹噹前時間改變時,報警管理送出Intent.ACTION_TIME_CHANGED廣播接收機;您需要重新計算您的顯示時間以及下次更新何時響應該廣播。

+1

那麼,什麼事件觸發時,小部件變得可見? –

+2

據我所知,沒有辦法檢測小部件變得可見的時刻。 – Pavel

相關問題