Android---60---Notification 通知欄的簡單使用,androidnotification

來源:互聯網
上載者:User

Android---60---Notification 通知欄的簡單使用,androidnotification

Notification是顯示在手機狀態列的通知 


通過Notification.Builder類建立Notification對象。

Notification.Builder常用方法:

setDefaults ():設定通知LED燈、音樂、震動等

setAutoCancle():設定點擊通知後,狀態列自動刪除通知

setContentTitle():設定通知標題

setContentText():設定通知內容

setSmallcon():設定小表徵圖

setLargecon():設定大表徵圖

setTick():設定通知在狀態列的提示為本

setContentIntent ():設定點擊通知後將要啟動的程式組件對應的PendingIntent

setWhen ():設定通知發布的時間

 

步驟:

1.調用getSystemService(NOTIFICATION_SERVICE)方法擷取系統的NotificationManager方法

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


2.建立一個Notification.Builder對象

Notification.Builder builder = new Notification.Builder(MainActivity.this);


3.為builder設定各種屬性

4.建立一個Notification對象
Notification notification = builder.build();

5.通過NotificationManager的notify方法發送Notification 
manager.notify(ID, notification);

 

Demo:

 

Activity:

public class MainActivity extends Activity {Button send, del;NotificationManager manager;int ID = 0x123;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);send = (Button) findViewById(R.id.send);del = (Button) findViewById(R.id.del);manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, other.class);PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0, intent, 0);Notification.Builder builder = new Notification.Builder(MainActivity.this);builder// Notification notification = new// Notification.Builder(MainActivity.this)// 設定開啟通知,該通知取消.setAutoCancel(true)// 設定通知提示資訊.setTicker("有新訊息")// 設定通知的表徵圖.setSmallIcon(R.drawable.pig)// 設定通知的標題.setContentTitle("不好了!!!")// 設定通知的內容.setContentText("你家豬跑了")// 設定使用系統預設的聲音、LED.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)// 設定通知發布時間.setWhen(System.currentTimeMillis())// 設定將要啟動的活動.setContentIntent(pi).build();Notification notification = builder.build();manager.notify(ID, notification);}});del.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {manager.cancel(ID);}});}}


 

 

 

點擊發送通知:

 


 

 

點擊該通知會跳轉到另一個活動:

 

聯繫我們

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