Android Notification通知詳解
002
003根據activity的生命週期,在activity不顯示時,會執行onStop函數(好比按下home鍵),因此你在onStop函數(按退出鍵除外)裏面把notification放在通知欄裏,再此顯示時,把notification從通知欄裏去掉。或者,只要程序在運行就一直顯示通知欄圖標。
004
005
006
007下面對Notification類中的一些常量,字段,方法簡單介紹一下:
008常量:
009DEFAULT_ALL 使用全部默認值,好比聲音,震動,閃屏等等
010DEFAULT_LIGHTS 使用默認閃光提示
011DEFAULT_SOUNDS 使用默認提示聲音
012DEFAULT_VIBRATE 使用默認手機震動
013【說明】:加入手機震動,必定要在manifest.xml中加入權限:
014<uses-permission android:name="android.permission.VIBRATE" />
015以上的效果常量能夠疊加,即經過
016notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;
017notification.defaults |= DEFAULT_SOUND (最好在真機上測試,震動效果模擬器上沒有)
018
019
020
021//設置flag位
022FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉
023FLAG_NO_CLEAR 該通知能被狀態欄的清除按鈕給清除掉
024FLAG_ONGOING_EVENT 通知放置在正在運行
025FLAG_INSISTENT 是否一直進行,好比音樂一直播放,知道用戶響應
026
027
028
029經常使用字段:
030contentIntent 設置PendingIntent對象,點擊時發送該Intent
031defaults 添加默認效果
032flags 設置flag位,例如FLAG_NO_CLEAR等
033icon 設置圖標
034sound 設置聲音
035tickerText 顯示在狀態欄中的文字
036when 發送此通知的時間戳
037
038
039
040NotificationManager經常使用方法介紹:
041public void cancelAll() 移除全部通知(只是針對當前Context下的Notification)
042public void cancel(int id) 移除標記爲id的通知 (只是針對當前Context下的全部Notification)
043public void notify(String tag ,int id, Notification notification) 將通知加入狀態欄,標籤爲tag,標記爲id
044public void notify(int id, Notification notification) 將通知加入狀態欄,標記爲id
045
046
047
048?
049package com.ljq.activity;
050
051import android.app.Activity;
052import android.app.Notification;
053import android.app.NotificationManager;
054import android.app.PendingIntent;
055import android.content.Intent;
056import android.graphics.Color;
057import android.os.Bundle;
058
059public class MainActivity extends Activity {
060 /** Called when the activity is first created. */
061 @Override-http://www.huiyi8.com/vi/
062 public void onCreate(Bundle savedInstanceState) {
063 super.onCreate(savedInstanceState);
064 setContentView(R.layout.main);
065 clearNotification();
066 }
067
068 @Override
069 protected void onStop() {
070 showNotification();
071 super.onStop();
072 }
073
074 @Override
075 protected void onStart() {
076 clearNotification();
077 super.onStart();
078 }
079
080 /**
081 * 在狀態欄顯示通知
082 */VI模板大全
083 private void showNotification(){
084 // 建立一個NotificationManager的引用
085 NotificationManager notificationManager = (NotificationManager)
086 this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
087
088 // 定義Notification的各類屬性
089 Notification notification =new Notification(R.drawable.icon,
090 "督導系統", System.currentTimeMillis());
091 //FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉
092 //FLAG_NO_CLEAR 該通知不能被狀態欄的清除按鈕給清除掉
093 //FLAG_ONGOING_EVENT 通知放置在正在運行
094 //FLAG_INSISTENT 是否一直進行,好比音樂一直播放,知道用戶響應
095 notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中
096 notification.flags |= Notification.FLAG_NO_CLEAR; // 代表在點擊了通知欄中的"清除通知"後,此通知不清除,常常與FLAG_ONGOING_EVENT一塊兒使用
097 notification.flags |= Notification.FLAG_SHOW_LIGHTS;
098 //DEFAULT_ALL 使用全部默認值,好比聲音,震動,閃屏等等
099 //DEFAULT_LIGHTS 使用默認閃光提示
100 //DEFAULT_SOUNDS 使用默認提示聲音
101 //DEFAULT_VIBRATE 使用默認手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />權限
102 notification.defaults = Notification.DEFAULT_LIGHTS;
103 //疊加效果常量
104 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
105 notification.ledARGB = Color.BLUE;
106 notification.ledOnMS =5000; //閃光時間,毫秒
107
108 // 設置通知的事件消息
109 CharSequence contentTitle ="督導系統標題"; // 通知欄標題
110 CharSequence contentText ="督導系統內容"; // 通知欄內容
111 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點擊該通知後要跳轉的Activity
112 PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
113 notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
114
115 // 把Notification傳遞給NotificationManager
116 notificationManager.notify(0, notification);
117 }
118?
119 //刪除通知
120 private void clearNotification(){
121 // 啓動後刪除以前咱們定義的通知
122 NotificationManager notificationManager = (NotificationManager) this
123 .getSystemService(NOTIFICATION_SERVICE);
124 notificationManager.cancel(0);
125
126 }
127}
android