Android監聽訊息通知欄點選事件

2021-10-01 04:59:47 字數 3382 閱讀 3558

使用broadcastreceiver

1 新建乙個notificationclickreceiver 類,並且在清單檔案中註冊!!

public

class

notificationclickreceiver

extends

broadcastreceiver

}

在清單檔案中註冊

android:name

=".notificationclickreceiver"

>

receiver

>

在你需要建立通知欄的地方

notificationmanager notificationmanager =

(notificationmanager)

this

.getsystemservice

(context.notification_service)

; notification.builder builder1 =

newnotification.builder

(mainactivity.

this);

builder1.

setsmallicon

(r.drawable.ic_launcher)

;//設定圖示

builder1.

setticker

("顯示第二個通知");

builder1.

setcontenttitle

("通知");

//設定標題

builder1.

setcontenttext()

;//訊息內容

builder1.

setwhen

(system.

currenttimemillis()

);//傳送時間

builder1.

setdefaults

(notification.default_all)

;//設定預設的提示音,振動方式,燈光

builder1.

setautocancel

(true);

//開啟程式後圖示消失

intent intent =

newintent

(mainactivity.

this

,notificationclickreceiver.

class);

pendingintent pendingintent =pendingintent.

getbroadcast

(mainactivity.

this,0

, intent,0)

; builder1.

setcontentintent

(pendingintent)

; notification notification1 = builder1.

build()

; notificationmanager.

notify

(124

, notification1)

;// 通過通知管理器傳送通知

如果需要攜帶什麼引數就在這裡的intent包裹即可,notificationclickreceiver可以接收到傳送過來的intent

相容android 8及以上

// 版本公升級通知框

notificationmanager notificationmanager =

(notificationmanager) mapactivity.

this

.getsystemservice

(context.notification_service)

; notification.builder builder1 =

newnotification.builder

(mapactivity.

this);

// 通知框相容 android 8 及以上

if(build.version.sdk_int >= build.version_codes.o)

builder1.

setsmallicon

(r.mipmap.touxiang)

;//設定圖示

builder1.

setcontenttitle

("這是乙個通知");

//設定標題

builder1.

setcontenttext

("這是訊息內容");

//訊息內容

builder1.

setwhen

(system.

currenttimemillis()

);//傳送時間

builder1.

setdefaults

(notification.default_all)

;//設定預設的提示音,振動方式,燈光

builder1.

setautocancel

(true);

//開啟程式後圖示消失

;// 通過通知管理器傳送通知

public

class

notificationclickreceiver

extends

broadcastreceiver

}

實現Android的訊息通知欄

背景知識 可以用activity和service來開始訊息通知,兩者的區別在於乙個是在前台觸發,乙個是後台服務觸發。要使用訊息通知,必須要用到兩個類 notificationmanager和notification,其他notificationmanager的初始化是用getsystemservic...

實現Android的訊息通知欄

背景知識 可以用activity和service來開始訊息通知,兩者的區別在於乙個是在前台觸發,乙個是後台服務觸發。要使用訊息通知,必須要用到兩個類 notificationmanager和notification,其他notificationmanager的初始化是用getsystemservic...

autojs通知欄 監聽通知欄失敗,請教大佬!

該樓層疑似違規已被系統摺疊 隱藏此樓檢視此樓 這個是自帶的監控通知欄示例,執行之後會有錯誤提示,請問各位大佬,這個是什麼情況?auto events.observenotification events.onnotification function notification printnotifi...