Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 實現Android的消息通知欄

實現Android的消息通知欄

編輯:關於android開發

  背景知識:可以用Activity和Service來開始消息通知,兩者的區別在於一個是在前台觸發,一個是後台服務觸發。

  要使用消息通知,必須要用到兩個類:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,並且通過notify方法來向android系統發送消息欄通知和顯示。

  效果:

  代碼:

  //消息通知欄

          //定義NotificationManager

          String ns = Context.NOTIFICATION_SERVICE;

          NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

          //定義通知欄展現的內容信息

          int icon = R.drawable.icon;

          CharSequence tickerText = "我的通知欄標題";

          long when = System.currentTimeMillis();

          Notification notification = new Notification(icon, tickerText, when);

        

          //定義下拉通知欄時要展現的內容信息

          Context context = getApplicationContext();

          CharSequence contentTitle = "我的通知欄標展開標題";

          CharSequence contentText = "我的通知欄展開詳細內容";

          Intent notificationIntent = new Intent(this, BootStartDemo.class);

          PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

          notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent); 

          //用mNotificationManager的notify方法通知用戶生成標題欄消息通知

          mNotificationManager.notify(1, notification);

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved