這實際上取決於何時/何時/如何顯示此圖像。我將假設圖像顯示在正常Activity
:
查看使用AlarmManager
設置警報。這些警報可能會啓動Service
更改圖像源(可能更新圖像資源或文件名的數據庫條目)。在Service
中完成工作後,請使用Intent.putExtra()
發送Broadcast
(或StickyBroadcast
),結果爲Bundle
。您也可以使用NotificationManager
在此處設置狀態欄通知。 Notification
的PendingIntent
應該是顯示圖像的Activity
。
退房「報警」和APIDemos(/samples/android-8/ApiDemos/src/com/example/android/apis/app
)的「通知」部分
更新:更健壯的報警系統是馬克·墨菲WakefulIntentService
發現here。
然後,在Activity
註冊BroadcastReceiver
,聽您在Service
設置Broadcast
Intent
。這將監聽服務何時完成。如果在Service
中使用StickyBroadcast
,則數據將被緩存,使其在Activity
到達堆棧的前端時可用。否則,您將需要保存數據(在SQLite數據庫或SharedPrefernces
中)並使用時間戳。
UPDATE - 展示持久性數據存儲:
一個偉大的地方開始學習SQLite的是Android Note Pad demo。這將教你Android的許多關鍵方面,包括生命週期,ContentProviders
(db包裝)和使用Android Views
(小工具)。
我提到的替代方法是使用應用程序的默認SharedPreferences
。一個簡單的例子是這樣的:
String imageUri = null;
long timestamp = 0;
final String currentImage = "current_image";
final String lastTimestamp = "last_timestamp"
// Access the default SharedPreferences
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
// The SharedPreferences editor - must use commit() to submit changes
SharedPreferences.Editor editor = preferences.edit();
// Get the current image URI
if (preferences.contains(currentImage) {
imageUri = preferences.getString(currentImage, null);
}
// Get the last timestamp
if (preferences.contains(lastTimestamp) {
timestamp = preferences.getLong(lastTimestamp, 0);
}
// To set the SharedPreferences
editor.putString(currentImage, imageUri);
editor.putLong(lastTimestamp, timestamp);
editor.commit();
希望我正確理解你,這有助於。
感謝Paul的回覆。我很確定我正在追蹤你的故障。我會按照您的建議查看警報和通知部分。麻煩你詳細說明的東西是堅持數據並使用時間戳。 – Wade 2010-09-18 21:03:55