2010-09-10 66 views

回答

26

的Jovan,這裏是製作兼容代碼2.0+和1.6倍的方法(代碼展示瞭如何檢測哪一個是兼容) http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html

爲2.0+,我把一些示例代碼(使用startForeground)。注意到一些代碼已被棄用,但Notification.Builder使用API​​級別11(3.x),這意味着在大多數手機使用兼容的Android版本之前,我不會使用它。由於絕大多數手機正在運行某個版本的2.x,因此我認爲跳過兼容性檢查足夠安全。

final static int myID = 1234; 

//The intent to launch when the user clicks the expanded notification 
Intent intent = new Intent(this, SomeActivityToLaunch.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0); 

//This constructor is deprecated. Use Notification.Builder instead 
Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis()); 

//This method is deprecated. Use Notification.Builder instead. 
notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent); 

notice.flags |= Notification.FLAG_NO_CLEAR; 
startForeground(myID, notice); 

放之內爲您服務onStartCommand()這個代碼,你是好去。 (但你可以把這段代碼放在你的服務的任何地方。)

P.S.停止前臺服務只需在您的服務中隨時隨地使用stopForeground(true);

+0

爲什麼需要爲在前臺運行的服務設置FLAG_NO_CLEAR?有沒有辦法「清除」前臺服務的通知?我知道AUTO_CANCEL [不起作用](http://stackoverflow.com/questions/4561521/android-notification-flag-auto-cancel-not-working)。 – Eduardo 2012-04-02 10:44:40

+1

爲什麼不使用支持庫中提供的通知構建器compat來獲取新API的優點,保持兼容性,而不是寫入棄用的代碼? – LoungeKatt 2013-08-07 17:01:19

+0

@TwistedUmbrella:Android仍然在不斷髮展中......在編寫Android支持庫時並沒有提供這種功能。 – 2013-08-20 21:22:48

0

我不能完全肯定,如果這是你在尋找什麼,但你所要做的就是創建一個通知對象(以下mOngoingNote),並使用通知ID與一起叫startForeground實際通知。

startForeground(NOTEMAN_ID, mOngoingNote); 
5

此代碼將通過將Android支持庫添加到您的項目中來使用適用於任何API的最佳選項。在Eclipse中,您可以右鍵單擊該項目,轉至Android工具,然後單擊「添加支持庫...」選項以下載並添加它。

final static int myID = 1234; 

//The intent to launch when the user clicks the expanded notification 
Intent intent = new Intent(this, SomeActivityToLaunch.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0); 

if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.DONUT) { 
// Build.VERSION.SDK_INT requires API 4 and would cause issues below API 4 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setTicker("TICKER").setContentTitle("TITLE").setContentText("CONTENT") 
      .setWhen(System.currentTimeMillis()).setAutoCancel(false) 
      .setOngoing(true).setPriority(Notification.PRIORITY_HIGH) 
      .setContentIntent(pendIntent); 
    Notification notification = builder.build(); 

} else { 

    Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis()); 
    notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent); 

} 
notification.flags |= Notification.FLAG_NO_CLEAR; 
startForeground(myID, notification); 

將代碼放在start方法使用的是爲您服務,那麼當你想前臺進程停止,任何地方調用stopForeground(true);在服務

+3

您錯過了'builder.setSmallIcon(int)'。沒有那個自定義構建器會被默認通知忽略。 – 2014-02-26 09:19:04

+0

問題不在於如何實現自定義通知。這是如何使用startForeground()方法。你做什麼以上是你的個人喜好。 – LoungeKatt 2014-03-03 04:07:52