溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中Notification的作用是什么

發(fā)布時間:2021-06-26 15:25:18 來源:億速云 閱讀:207 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關(guān)Android中Notification的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Android的系統(tǒng)有一個通知欄,這個通知欄很管用。通知是Android系統(tǒng)跟用戶交互的一個重要的部分。比如有新郵件,新短信息,未接來電等等信息都會顯示在通知欄。我們把通知欄拖下來,點擊通知信息,就能顯示通知的具體內(nèi)容。很cool的功能。

我們也可以定義通知的樣式,有圖標(biāo),通知的聲音,還有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:         //獲得通知管理器,通知是一項系統(tǒng)服務(wù)  48:         final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  49:         //初始化通知對象 p1:通知的圖標(biāo) p2:通知的狀態(tài)欄顯示的提示 p3:通知顯示的時間  50:         Notification notification = new Notification(R.drawable.icon, "通知測試", System.currentTimeMillis());  51:         //點擊通知后的Intent,此例子點擊后還是在當(dāng)前界面  52:         PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationDemo.class), 0);  53:         //設(shè)置通知信息  54:         notification.setLatestEventInfo(this, "通知title", "通知信息內(nèi)容", intent);  55:         //通知  56:         manager.notify(NOTICE_ID, notification);  57:     }  58: }

看完上述內(nèi)容,你們對Android中Notification的作用是什么有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI