Android學習—Notification訊息通知

來源:互聯網
上載者:User

標籤: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訊息通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.