對於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();
}