歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android入門學習—Notification消息通知

Android入門學習—Notification消息通知

日期:2017/3/1 9:30:17   编辑:Linux編程

最近在項目中需要使用消息通知,自己把它封裝成了一個方法,需要的時候方便調用,
下面對Notification類中的一些常量,字段,方法簡單介紹一下:
常量:
DEFAULT_ALL 使用所有默認值,比如聲音,震動,閃屏等等
DEFAULT_LIGHTS 使用默認閃光提示
DEFAULT_SOUNDS 使用默認提示聲音
DEFAULT_VIBRATE 使用默認手機震動
【說明】:加入手機震動,一定要在manifest.xml中加入權限:
<uses-permission Android:name="android.permission.VIBRATE" />
以上的效果常量可以疊加,即通過
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;
notification.defaults |= DEFAULT_SOUND (最好在真機上測試,震動效果模擬器上沒有)


//設置flag位 FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉
FLAG_NO_CLEAR 該通知能被狀態欄的清除按鈕給清除掉
FLAG_ONGOING_EVENT 通知放置在正在運行
FLAG_INSISTENT 是否一直進行,比如音樂一直播放,知道用戶響應
常用字段:
contentIntent 設置PendingIntent對象,點擊時發送該Intent
defaults 添加默認效果
flags 設置flag位,例如FLAG_NO_CLEAR等
icon 設置圖標
sound 設置聲音
tickerText 顯示在狀態欄中的文字
when 發送此通知的時間戳
/*******************************************分割線************************************************/
貼上源代碼:
123456789101112131415161718 private void showNotification(CharSequence Title,CharSequence Text){
//獲得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//構建一個通知對象(需要傳遞的參數有三個,分別是圖標,標題和 時間)
Notification notification = new Notification(R.drawable.logo_notify,Title,System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;//點擊後自動消失
notification.defaults = Notification.DEFAULT_SOUND;//聲音默認
//定義下拉通知欄時要展現的內容信息
Context context = getApplicationContext();
//點擊該通知後要跳轉的Activity
Intent intent = new Intent(this,Target.class);
BudgetSetting.budgetFlag="Setting";
PendingIntent pendingIntent = PendingIntent.getActivity(AccountAdding.this,0,intent,0); notification.setLatestEventInfo(getApplicationContext(), "通知標題", "通知顯示的內容", pendingIntent);
notification.setLatestEventInfo(context, Title, Text, pendingIntent);
//用mNotificationManager的notify方法通知用戶生成標題欄消息通知
manager.notify(1, notification);
finish();
}

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved