Android Notification通知

來源:互聯網
上載者:User

標籤:android   style   blog   io   ar   color   使用   sp   java   

 1 /** 2      * 在狀態列顯示通知 3      */ 4     private void showNotification(){  5         // 建立一個NotificationManager的引用    6         NotificationManager notificationManager = (NotificationManager)     7             this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);    8            9         // 定義Notification的各種屬性   10         Notification notification =new Notification(R.drawable.icon,   11                 "督導系統", System.currentTimeMillis()); 12         //FLAG_AUTO_CANCEL   該通知能被狀態列的清除按鈕給清除掉13         //FLAG_NO_CLEAR      該通知不能被狀態列的清除按鈕給清除掉14         //FLAG_ONGOING_EVENT 通知放置在正在運行15         //FLAG_INSISTENT     是否一直進行,比如音樂一直播放,知道使用者響應16         notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中   17         notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點擊了通知欄中的"清除通知"後,此通知不清除,經常與FLAG_ONGOING_EVENT一起使用   18         notification.flags |= Notification.FLAG_SHOW_LIGHTS;   19         //DEFAULT_ALL     使用所有預設值,比如聲音,震動,閃屏等等20         //DEFAULT_LIGHTS  使用預設閃光提示21         //DEFAULT_SOUNDS  使用預設提示聲音22         //DEFAULT_VIBRATE 使用預設手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />許可權23         notification.defaults = Notification.DEFAULT_LIGHTS; 24         //疊加效果常量25         //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;26         notification.ledARGB = Color.BLUE;   27         notification.ledOnMS =5000; //閃光時間,毫秒28           29         // 設定通知的事件訊息   30         CharSequence contentTitle ="督導系統標題"; // 通知欄標題   31         CharSequence contentText ="督導系統內容"; // 通知欄內容   32         Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點擊該通知後要跳轉的Activity   33         PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);   34         notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);   35           36         // 把Notification傳遞給NotificationManager 37         notificationManager.notify(0, notification);   
    38 }


  //刪除通知        private void clearNotification(){        // 啟動後刪除之前我們定義的通知          NotificationManager notificationManager = (NotificationManager) this                .getSystemService(NOTIFICATION_SERVICE);           notificationManager.cancel(0);       }
NotificationManager常用方法介紹: public void cancelAll() 移除所有通知(只是針對當前Context下的Notification) public  void cancel( int id) 移除標記為id的通知 (只是針對當前Context下的所有Notification) public  void notify(String tag , int id, Notification notification) 將通知加入狀態列,標籤為tag,標記為id public  void notify( int id, Notification notification) 將通知加入狀態列,標記為id      

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.