我正在創建一個android服務,它將在設備引導過程完成後開始運行。在我的服務中,我正在創建一項任務。根據某些條件,此任務將隨時啓動或停止。我的意圖是每當我開始我的任務時,我想在狀態欄中顯示一個圖標,以知道我的任務正在運行,就像藍牙圖標在打開時顯示一樣。當我的任務正在運行時在狀態欄中顯示一個圖標
回答
您可以像使用 Custom title with image
自定義標題欄,檢查偏好設置您的服務是啓用或禁用,並設置自定義標題。但在此之前請閱讀:http://developer.android.com/guide/topics/ui/notifiers/notifications.html
自定義標題與狀態欄圖標不同。實際上,在我的應用程序中沒有任何活動,它的一項服務在設備啓動後自動啓動。是否有可能使用自定義標題沒有在應用程序中的活動?無論如何感謝您的答案。 – 2012-01-12 13:16:55
是的,它可能在沒有任何活動的情況下啓動服務......只需將您的服務作爲前臺啓動即可。你會提供一個通知對象,以便開始服務 – waqaslam 2012-01-12 13:25:06
您需要一個Notification。代碼在談論:)
在您的服務:
private NotificationManager mNM;
private int NOTIFICATION = 10002; //Any unique number for this notification
要顯示通知:
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.status_icon, text, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
,並隱藏它,你只需做到這一點:
mNM.cancel(NOTIFICATION); //The same unique notification number.
這裏是一些澄清:
R.drawable.status_icon
:通知圖標R.string.local_service_started
:通知標題R.string.local_service_label
:通知最新信息(小標題)MainActivity.class
:當用戶單擊該通知
我認爲通知不同於狀態欄圖標。我提到像狀態欄上顯示的藍牙圖標。我認爲你的答案不適合我的要求。無論如何感謝回答我的問題。 – 2012-01-24 09:41:52
你的意思是像用戶永遠不會與之交互的圖標?像3G圖標,同步和耳機? – iTurki 2012-01-24 09:44:55
是的,應用程序可以嗎? – 2012-01-24 09:45:54
- 1. 我怎麼能顯示一個JavaScript的加載欄來顯示正在運行的javascript的狀態
- 2. Qt正常狀態欄在臨時狀態後不顯示
- 3. 顯示時的狀態正在等待掛起的圖標,顯示完整的圖標時的狀態完成
- 4. 如何在emacs狀態欄上顯示正常運行時間?我正在尋找/ usr/bin/uptime輸出顯示在狀態欄
- 5. Eclipse正在顯示處於等待狀態的每個任務
- 6. 當我在多任務欄中殺死應用程序並運行程序時,它的狀態與我正在關閉應用程序的狀態相同
- 7. 如何在應用程序運行時在狀態欄中顯示圖標,包括在背景中?
- 8. 通知圖標不顯示在棒棒糖的狀態欄中
- 9. 任務欄中的UWP App圖標無法正常顯示
- 10. 如何在Android狀態欄通知中顯示正確的當前時間?
- 11. 顯示狀態欄暫時
- 12. 不能顯示視圖狀態欄,一個UIWindow,目標C
- 13. 在通知欄中顯示一個窗口服務的圖標?
- 14. 如何在狀態欄上顯示播放指示圖標?
- 15. 在任務欄中顯示隱藏窗口的圖標
- 16. 在macvim的狀態行中顯示`expandtab`的當前狀態
- 17. 如何顯示在狀態欄上一個通知圖標多個通知
- 18. 如何在我的iPhone/iPad的狀態欄中顯示播放圖標
- 19. 顯示一個沒有顯示在任務欄上的ContextMenuStrip
- 20. 顯示任務運行狀態的HTML5/CSS3佈局
- 21. 當另一個任務正在運行時運行進度條android
- 22. 狀態欄顯示在我的視圖的邊界上iOS 7
- 23. 如何在任務欄中顯示子窗口圖標
- 24. 狀態欄顯示在Android上,當鍵盤可見時
- 25. 狀態欄中的圖標
- 26. PhoneGap 2.7在狀態欄中顯示activityindicator
- 27. 在狀態欄中顯示異常
- 28. 在Lwuit中顯示狀態欄J2ME
- 29. 運行時隱藏/顯示狀態欄iOS 9+
- 30. IndexOutofBoundException在我的地圖類正在運行時顯示
可能將要推出的活動[如何在應用程序運行時在狀態欄中顯示圖標,包括在背景中?]的副本(http://stackoverflow.com/questions/3973208/how-to-show-an-icon-in-the- status-bar-when-application-is-running-in-in) – 2016-02-12 19:22:50