1 自定义一个方法弹出通知栏

private void notifyCust() {

notId++;

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

notificationChannel.setDescription("通知...");

notificationChannel.enableLights(true);

notificationManager.createNotificationChannel(notificationChannel);

}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

builder.setSmallIcon(R.drawable.group_msg_off);

RemoteViews remoteViews = new RemoteViews(getApplication().getPackageName(), R.layout.notification_cust_layout);

remoteViews.setTextViewText(R.id.tv_notification_cust_layout_title, "通知标题");

remoteViews.setImageViewResource(R.id.iv_notification_cust_layout_icon, R.drawable.group_msg_off);

Intent intentClick = new Intent(this, CustomNotificationBroadcastReceiver.class);

intentClick.setAction("notification_clicked");

intentClick.putExtra(CustomNotificationBroadcastReceiver.TYPE, notId);

PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_ONE_SHOT);

Intent intentCancel = new Intent(this, CustomNotificationBroadcastReceiver.class);

intentCancel.setAction("notification_cancelled");

intentCancel.putExtra(CustomNotificationBroadcastReceiver.TYPE, notId);

PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);

builder.setContentIntent(pendingIntentClick);

builder.setDeleteIntent(pendingIntentCancel);

Notification notification = builder.build();

notification.contentView = remoteViews;

notificationManager.notify(notId, notification);

}

2 自定义布局notification_cust_layout.xml

android:layout_width="match_parent"

android:layout_height="60dp"

android:id="@+id/ll_notification_cust_layout"

android:gravity="center_vertical"

android:orientation="horizontal">

android:id="@+id/iv_notification_cust_layout_icon"

android:layout_width="35dp"

android:layout_height="35dp"

android:layout_marginLeft="15dp"

android:src="@drawable/add_facility" />

android:textColor="#000000"

android:id="@+id/tv_notification_cust_layout_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="15dp"

android:text="标题" />

3 自定义广播接收器

package com.fsh.lfmf.activity.video.not;

import android.app.NotificationManager;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

/**

* @author songkai

* @date on 2019/4/23

* 自定义通知栏的广播接收器

*/

public class CustomNotificationBroadcastReceiver extends BroadcastReceiver {

public static final String TYPE = "type";

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

int type = intent.getIntExtra(TYPE, -1);

if (type != -1) {

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(type);

}

if ("notification_clicked".equals(action)) {

Log.d("Notification", "onReceive: -->点击了通知栏");

}

if ("notification_cancelled".equals(action)) {

Log.d("Notification", "onReceive: -->通知栏取消了");

}

}

}

4 在配置文件进行广播配置项

本文由用户 【好好学习】 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。

转载本站原创文章,请注明出处,并保留原始链接、图片水印。

本站是一个以用户分享为主的开源技术平台,欢迎各类分享!

分享:

android通知栏的点击事件,android自定义通知栏以及点击事件相关推荐

  1. Android初级基础知识复习(十八) —— 自定义通知栏

    通知推送Notification 在手机屏幕的顶端下拉会弹出通知栏,里面存放的是App即时提醒用户的消息,消息内容由Notification 产生并推送.每条消息通知基本都有图标.标题.内容.时间等元 ...

  2. jbutton添加点击事件_electron-vue自定义边框后点击事件失效问题

    问题描述:electron-vue自定义边框后点击事件失效(不单单点击事件,窗体内所有事件都失效) 解决方案:在需要事件处理的节点上添加样式-webkit-app-region:no-drag; sr ...

  3. android:performClick的用法——自定义View增加点击事件更配performClick噢!

    文章目录 浅析 如何使用 模拟点击事件 配合自定义View 浅析 performClick 官方注释 Call this view's OnClickListener, if it is define ...

  4. Android自定义View,滑动,事件传递小结

    本文只总结知识点 欢迎补充,欢迎纠正.谢谢! #预备知识 Android控件框架 ####1. View树状图 Android的View树结构总是以一个ViewGroup开始,包含多个View或Vie ...

  5. android html图片点击事件,Android TextView加载HTMl图文之添加点击事件和查看图片

    前言 用TextView显示Html图文,每一个需求都是需要探索的,不再是简单的添加点击事件就可以了. 1.如何添加点击事件 这里要使用上在Html.forHtml()方法中的第四个参数:Html.T ...

  6. Android自定义通知栏显示

    概述 本文主要想说一下android项目开发中经常会使用到通知机制中的通知栏框架(Notificaiton).通知栏位于设备的顶层,可以展开的通知列表.通知栏的主要作用大概可以分为三大块: 显示未读的 ...

  7. android 拦截点击事件,Android事件拦截机制

    一直对事件拦截不是很清楚,读Android群英传的笔记,记录下. 要了解事件拦截,首先要了解触摸事件,触摸事件是捕获触摸屏幕后发生的事件.按一下屏幕通常会有几个事件发生,当按下屏幕,这是事件1.滑动了 ...

  8. android自定义通知栏图标大小,Android不使用自定义布局情况下实现自定义通知栏图标的方法...

    本文实例讲述了Android不使用自定义布局情况下实现自定义通知栏图标的方法.分享给大家供大家参考,具体如下: 自定义通知栏图标?不是很简单么.自定义布局都不在话下! 是的,有xml布局文件当然一切都 ...

  9. android 键盘回车按钮事件,Android中ENTER键(确认键)点击响应

    背景:在机顶盒中通过遥控器操作,与传统手机交互方式不同.手机点击是发送两个TouchEvent(Down和Up),机顶盒是发送KeyEvent.所产生的效果看似相同,其实是两种不同的机制. 先看两段代 ...

最新文章

  1. 往年包场丘赛的北大,今年被清华逆袭了
  2. java 8 stream()
  3. 046 实例11-自动轨迹绘制
  4. Linux学习-11月12日(Apache安装)
  5. 安卓案例:LayoutCreator演示
  6. 华为Mate 50系列明年初发布:麒麟990/骁龙8 Gen1加持
  7. windows 根据父进程pid查找所有子进程id(C++)
  8. hdoj 2122 Ice_cream’s world III【最小生成树】
  9. java前端vml_在Web中使用JavaScript和VML实现WebGIS中的测距
  10. (二十三)八部众(2)
  11. 计算机win2000如何连接wifi,台式电脑windows7怎么连接wifi
  12. 小程序源码:求职招聘微信小程序源码下载v4.1.78
  13. 下载学习通上没有下载权限的文件
  14. 阿里云IoTStudio中的“移动可视化开发” 重新登场了--让开发APP不再难
  15. Pr 视频效果:调整
  16. 二级C语言上机考试评分标准,浅谈二级C语言上机考试评分原理
  17. java ascii码转字符_java中char对应的ASCII码的转化操作
  18. 解决程序缺少richtx32.ocx无法运行问题
  19. MapInfo上的GIS系统开发
  20. zebra 线程流具体分析

热门文章

  1. Mac取证你需要了解的那些事!
  2. [yishen] 小慕读书web端学习笔记
  3. 八哥英语词根词缀汇总
  4. 判断三角形的性质(直角或等腰)简便算法
  5. iMazing2023iOS系统设备数据传输与备份工具使用教程
  6. 腾讯优图TFace正式开源,更可信的人脸识别!
  7. PLC通讯之串口转以太网/WIFI的透传模块(DTU)的专用OPC软件和DLL通讯组件
  8. 重磅!全球前2%顶尖科学家榜单发布!
  9. 断链在平曲线计算中的处理——短链篇
  10. 如何让自己的博客主动被搜索引擎收录