回答
的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);
我要尋找的實施startForeground對Android 2.0的,示例是在API級別5之前執行的。感謝 – Jovan 2010-09-10 18:53:08
ID的用途是什麼? – 2016-02-06 22:29:37
我不能完全肯定,如果這是你在尋找什麼,但你所要做的就是創建一個通知對象(以下mOngoingNote
),並使用通知ID與一起叫startForeground
實際通知。
startForeground(NOTEMAN_ID, mOngoingNote);
此代碼將通過將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);
在服務
您錯過了'builder.setSmallIcon(int)'。沒有那個自定義構建器會被默認通知忽略。 – 2014-02-26 09:19:04
問題不在於如何實現自定義通知。這是如何使用startForeground()方法。你做什麼以上是你的個人喜好。 – LoungeKatt 2014-03-03 04:07:52
- 1. 如何在android中實現notifysetDatachanged方法?
- 2. 無法在類中實現方法聲明實現通用接口方法
- 3. Android Studio - 如何在所有實現類中實現接口方法?
- 4. 無法實現在Android 2.2
- 5. Android在實現onClickListener時在外部方法中更改onClickListener
- 6. Android onTouchListener,更簡單的實現方法
- 7. 自動實現方法Kotlin Android
- 8. 實現在Android中
- 9. 無法在android webview中實現javascript
- 10. 無法在Android中實現回調
- 11. 如何在對話框中實現onWindowFocusChange()方法(Android)
- 12. 在Android中實現類似TimerTask的最佳方法
- 13. 在android中實現服務的正確方法
- 14. 在Ruby中實現一種類方法
- 15. 在Ruby中實現訪問器方法
- 16. 方法在servicestack redis中沒有實現
- 17. 在Java Hashmap中實現remove方法?
- 18. 在File.Delete方法中實現IDisposable?
- 19. 如何在IBAction中實現NSArray方法?
- 20. 如何在VB.NET中實現GetStableHash方法
- 21. 在javascript中實現擴展方法
- 22. 在Scala中實現Java接口方法
- 23. 在java中實現抽象方法/類
- 24. 在java中實現equals方法
- 25. 在C++中註冊實現的方法
- 26. 工廠方法在actionscript中的實現
- 27. 在scala類中實現哈希方法
- 28. 如何在TypeScript中實現clone()方法?
- 29. 在方法中實現按鈕onClickListener
- 30. 如何在c#中實現Array.ConvertAll方法
爲什麼需要爲在前臺運行的服務設置FLAG_NO_CLEAR?有沒有辦法「清除」前臺服務的通知?我知道AUTO_CANCEL [不起作用](http://stackoverflow.com/questions/4561521/android-notification-flag-auto-cancel-not-working)。 – Eduardo 2012-04-02 10:44:40
爲什麼不使用支持庫中提供的通知構建器compat來獲取新API的優點,保持兼容性,而不是寫入棄用的代碼? – LoungeKatt 2013-08-07 17:01:19
@TwistedUmbrella:Android仍然在不斷髮展中......在編寫Android支持庫時並沒有提供這種功能。 – 2013-08-20 21:22:48