Android開發通知Notification

來源:互聯網
上載者:User

Android的系統有一個通知欄,這個通知欄很管用。通知是Android系統跟使用者互動的一個重要的部分。比如有新郵件,新簡訊息,未接來電等等資訊都會顯示在通知欄。我們把通知欄拖下來,點擊通知資訊,就能顯示通知的具體內容。很cool的功能。

我們也可以定義通知的樣式,有表徵圖,通知的聲音,還有LED的顏色,閃爍的頻率等等。

我們看一下例子

 1: package com.halzhang.android.notification;
   2:  
   3: import android.app.Activity;
   4: import android.app.Notification;
   5: import android.app.NotificationManager;
   6: import android.app.PendingIntent;
   7: import android.content.Intent;
   8: import android.os.Bundle;
   9: import android.view.View;
  10: import android.widget.Button;
  11: /**
  12:  * 通知
  13:  * @author 張漢國
  14:  */
  15: public class NotificationDemo extends Activity {
  16:     /** Called when the activity is first created. */
  17:     //通知id
  18:     private static final int NOTICE_ID = 1222;
  19:     private Button notify;
  20:     private Button cancel;
  21:     @Override
  22:     public void onCreate(Bundle savedInstanceState) {
  23:         super.onCreate(savedInstanceState);
  24:         setContentView(R.layout.main);
  25:         notify = (Button) findViewById(R.id.noti);
  26:         cancel = (Button) findViewById(R.id.cancel);
  27:         notify.setOnClickListener(new View.OnClickListener() {
  28:             
  29:             @Override
  30:             public void onClick(View v) {
  31:                 notityMe();
  32:             }
  33:         });
  34:         
  35:         cancel.setOnClickListener(new View.OnClickListener() {
  36:             
  37:             @Override
  38:             public void onClick(View v) {
  39:                 final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  40:                 //取消通知
  41:                 manager.cancel(NOTICE_ID);
  42:             }
  43:         });
  44:     }
  45:     
  46:     private void notityMe(){
  47:         //獲得通知管理器,通知是一項系統服務
  48:         final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  49:         //初始化通知對象 p1:通知的表徵圖 p2:通知的狀態列顯示的提示 p3:通知顯示的時間
  50:         Notification notification = new Notification(R.drawable.icon, "通知測試", System.currentTimeMillis());
  51:         //點擊通知後的Intent,此例子點擊後還是在當前介面
  52:         PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationDemo.class), 0);
  53:         //設定通知資訊
  54:         notification.setLatestEventInfo(this, "通知title", "通知資訊內容", intent);
  55:         //通知
  56:         manager.notify(NOTICE_ID, notification);
  57:     }
  58: }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.