Android监听消息通知栏点击事件
Android监听消息通知栏点击事件
使用BroadCastReceiver
1 新建一个NotificationClickReceiver 类,并且在清单文件中注册!!
public class NotificationClickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//todo 跳转之前要处理的逻辑
Log.i("TAG", "userClick:我被点击啦!!! ");
Intent newIntent = new Intent(context, Main2Activity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
}
}
在清单文件中注册
<receiver
android:name=".NotificationClickReceiver">
</receiver>
在你需要创建通知栏的地方
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder1 = new Notification.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 =new Intent (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 = new Notification.Builder(MapActivity.this);
// 通知框兼容 android 8 及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("11212313131", "NotificationName", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
builder1.setChannelId("123456");
}
builder1.setSmallIcon(R.mipmap.touxiang); //设置图标
builder1.setContentTitle("这是一个通知"); //设置标题
builder1.setContentText("这是消息内容"); //消息内容
builder1.setWhen(System.currentTimeMillis()); //发送时间
builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//打开程序后图标消失
Intent intent = new Intent(Activity.this, NotificationClickReceiver.class);
intent.putExtra("url","www.baidu.com");
PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();
notificationManager.notify(124, notification1); // 通过通知管理器发送通知
public class NotificationClickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getStringExtra("url");
Uri uri = Uri.parse(url);
Intent i = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(i);
}
}
Android监听消息通知栏点击事件的更多相关文章
- Android 监听按钮的点击事件
onClick事件1.Button和ImageButton都拥有一个onClick事件 通过自身的.setOnClickListener(OnClickListener)方法添加点击事件2.所有的控件 ...
- Android实现监听控件点击事件
Android实现监听控件点击事件 引言 这篇文章主要想写一下Android实现监听点击事件的几种方法,Activity和Fragment实现起来有些方法上会有些不同,这里也略做介绍. 最近一直在忙一 ...
- 监听tableview的点击事件
// 监听tablview的点击事件 - (void)addAGesutreRecognizerForYourView { UITapGestureRecognizer *tapGesture = [ ...
- highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小 大] | ...
- Android 使用 OnTouchListener 接口监听双击或多击事件
这里是使用 OnTouchListener 实现的监听双击 or 多击的监听器.通过 View.setOnTouchListener ,可以实现在任意 View 上监听双击事件. 网上有许多文章简单的 ...
- Android控件——监听按钮的点击事件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAroAAAFTCAIAAABZPDiZAAAgAElEQVR4nOy9918UWfb///1jdu2uBs
- Android 设置软键盘搜索键以及监听搜索键点击事件
如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮.调用的实现只需要在XML在输入框中加入android:imeOptions="actionSearch" ...
- cocos2d-x JS 弹出对话框触摸监听(吞噬点击事件遮挡层)
在游戏中,我们经常会碰到一些弹窗,这些弹窗禁止点透,也就是禁止触摸事件传递到底层,我们称之为遮挡层,这些遮挡层,需要开发遮挡层,我们首先得了解cocos2d-js的触摸传递机制. 根据官方文档,我们可 ...
- Android 监听软键盘点击回车及换行事件
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean ...
随机推荐
- oracle分组后取最新的记录
使用Group By来实现取最新记录,需要注意一个问题,如果最大时间相同的数据都会被取出来. PS:即使数据字段类型是timestamp,也会登录相同的时间的数据. select A.* from A ...
- python-flask模块注入(SSTI)
前言: 第一次遇到python模块注入是做ctf的时候,当时并没有搞懂原理所在,看了网上的资料,这里做一个笔记. flask基础: 先看一段python代码: from flask import fl ...
- PTA 1005 Spell It Right
题目描述: Given a non-negative integer N, your task is to compute the sum of all the digits of N, and ou ...
- learn to rank 模型概述
模型总体描述: https://zhuanlan.zhihu.com/p/26539920 LambdaMART中Lambda计算以及RegressionTree训练: https://blog.cs ...
- SpringBoot整合持久层技术--(二)MyBatis
简介: 原名iBatis,SpringBoot中使用MyBatis: pom.xml <dependency> <groupId>org.springframework.boo ...
- 【spring boot】SpringBoot初学(2.2)– SpEL表达式读取properties属性到Java对象
前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置:(注意测试方法在,test下的SpelValueApplicationTest.c ...
- python笔记09
今日内容 三元运算 函数 内容详细 三元运算(三目运算) v = 前面 if 条件 else 后面 if 条件: v = '前面' else: v = '后面' # 让用户输入值,如果值是整数,则转换 ...
- AI 数学基础 : 熵
什么是熵(entropy)? 1.1 熵的引入 事实上,熵的英文原文为entropy,最初由德国物理学家鲁道夫·克劳修斯提出,其表达式为: 它表示一个系系统在不受外部干扰时,其内部最稳定的状态.后来一 ...
- python3中的参数*args
python的传参是如何实现的 # 将未拆包的数据进行传参 def run(a,*args): #第一个参数传给了a print(a) # args是一个元组,里面是2和3两个参数 print(a ...
- Linux开机加载过程
2015-01-06 10:29:13 目录 1 开机加载简介 2 常规加载流程 2.1 加载BIOS 2.2 读取MBR 2.3 boot loader 2.4 加载内核 2.5 init依据i ...