對於android開發人員來說,總是希望他們開發的程序可以被用戶經常使用,所以最希望的就是在用戶可得見的地方直接進入,呵呵。。。狀態欄通知便是這樣的地方,像QQ或是360或是其他IM軟件。
那麼如何實現狀態欄通知呢?
簡單的一個方法搞定:
//消息管理器
private NotificationManager n_NotificationManager;
private PendingIntent n_PendingIntent;
//通知對象
private Notification n_Notification;
public void showNotification(int dwrid)
{
//初始化消息管理器
n_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//設置點擊通知時顯示內容的界面
n_PendingIntent = PendingIntent.getActivity(ICityActivity.this, 0, getIntent(), 0);
//通知對象
n_Notification = new Notification();
//設置通知在狀態欄顯示的圖標
n_Notification.icon = dwrid;
//當我們點擊通知時顯示的內容
n_Notification.tickerText = "阿基米正在啓動宇宙級裝備,請稍後.....";
//通知時發出默認的聲音
n_Notification.defaults = Notification.DEFAULT_SOUND;
//設置通知顯示的參數
n_Notification.setLatestEventInfo(ICityActivity.this, "阿基米", "阿基米,給您帶來無限的快樂", n_PendingIntent);
//可以理解爲執行這個通知
n_NotificationManager.notify(0, n_Notification);
}
在Activity啓動時調用它,並給一個圖標的id
記住在程序退出時:
protected void onDestroy() {
//清除它
n_NotificationManager.cancelAll();
super.onDestroy();
}