今晚再看 ...
來源
當我們寫好一支程式, 尤其是常駐型的程式,
多半我們要通知使用者, 除了鈴聲、振動外, 最常使用的應該就是在狀態列上
顯示所要表達的內容文字, 並且讓使用者可以點選後, 執行該相關的程式
底下是如何把訊息顯示在狀態列上
02 | NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
05 | Notification msg = new Notification( |
08 | System.currentTimeMillis() |
12 | PendingIntent intent = PendingIntent.getActivity( |
15 | new Intent(this, AndroidTuition.class), |
16 | PendingIntent.FLAG_UPDATE_CURRENT |
19 | msg.setLatestEventInfo(Test.this, "請注意 !!", "這是一顆足球....", intent); |
20 | nManager.notify(0, msg); |
首先, 必需要先有一個管理的物件來管理訊息提醒的功能 “NotificationManager”
這個管理的物件可直接經由 Activity 中直接呼叫 getSystemService(NOTIFICATION_SERVICE) 的 Method 來取得
若是要取得其它的功能, 例如 “鬧鈴”, 則可使用相同的方式來取得 AlarmManager
“getSystemService(ALARM_SERVICE);”
上述程式碼當中主要的功能有三段
1. 取得服務的管理物件
2. 在狀態列上顯示訊息
3. 在狀態欄中顯示訊息 (標題列下拉後的視窗)
而程式裡頭的 PendingIntent.getActivity() 是指當使用者點選狀態欄中的資料後,
系統該去執行哪一個 Activity
Labels: Android
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home