標籤:android notification
最近在項目中需要使用訊息通知,自己把它封裝成了一個方法,需要的時候方便調用,
下面對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對象,點擊時發送該Intentdefaults 添加預設效果flags 設定flag位,例如FLAG_NO_CLEAR等icon 設定表徵圖sound 設定聲音tickerText 顯示在狀態列中的文字when 發送此通知的時間戳記/*******************************************分割線************************************************/貼上原始碼:
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(); }
本文出自 “QYtag (Upspringing)” 部落格,請務必保留此出處http://qytag.blog.51cto.com/6125308/1590726
Android學習—Notification訊息通知