2012-01-12 31 views
5

我正在創建一個android服務,它將在設備引導過程完成後開始運行。在我的服務中,我正在創建一項任務。根據某些條件,此任務將隨時啓動或停止。我的意圖是每當我開始我的任務時,我想在狀態欄中顯示一個圖標,以知道我的任務正在運行,就像藍牙圖標在打開時顯示一樣。當我的任務正在運行時在狀態欄中顯示一個圖標

+0

可能將要推出的活動[如何在應用程序運行時在狀態欄中顯示圖標,包括在背景中?]的副本(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

回答

0

您可以像使用 Custom title with image

自定義標題欄,檢查偏好設置您的服務是啓用或禁用,並設置自定義標題。但在此之前請閱讀:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

自定義標題與狀態欄圖標不同。實際上,在我的應用程序中沒有任何活動,它的一項服務在設備啓動後自動啓動。是否有可能使用自定義標題沒有在應用程序中的活動?無論如何感謝您的答案。 – 2012-01-12 13:16:55

+0

是的,它可能在沒有任何活動的情況下啓動服務......只需將您的服務作爲前臺啓動即可。你會提供一個通知對象,以便開始服務 – waqaslam 2012-01-12 13:25:06

7

您需要一個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:當用戶單擊該通知
+0

我認爲通知不同於狀態欄圖標。我提到像狀態欄上顯示的藍牙圖標。我認爲你的答案不適合我的要求。無論如何感謝回答我的問題。 – 2012-01-24 09:41:52

+0

你的意思是像用戶永遠不會與之交互的圖標?像3G圖標,同步和耳機? – iTurki 2012-01-24 09:44:55

+0

是的,應用程序可以嗎? – 2012-01-24 09:45:54

相關問題