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 ...
随机推荐
- 剑指offer-面试题56_1-数组中只出现一次的两个数字-位运算
/* 题目: 求数组A中只出现一次的数字,该数组中有2个数字a.b仅出现一次,其余均出现两次 */ /* 思路: 两个相同的数字异或为0. 遍历数组,得到数组中各数字异或后的结果x,结果x=a^b. ...
- atcoder Keyence Programming Contest 2020 题解
比赛地址 A 题意:给一个\(n*m\)的初始为白色的矩阵,一次操作可以将一行或一列染成 黑色,问至少染出\(k\)个黑点的最少操作次数. \(n\),\(m\)<=100,\(k\)<= ...
- python3-cookbook笔记:第二章 字符串和文本
python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...
- VSCode添加git bash作为默认终端
VSC添加git bash作为默认终端的settings.json添加 { "terminal.integrated.shell.windows": "D:\\Progr ...
- beego控制器介绍
控制器介绍 提示:在 v1.6 中,此文档所涉及的 API 有重大变更,this.ServeJson() 更改为 this.ServeJSON(),this.TplNames 更改为 this.Tpl ...
- Java基础汇总2019
1.事务的ACID性: (1)原子性:要么做,要么都不做.程序操作执行未成功,则所做的更改会被撤销: (2)一致性:比如转账,a转给b一百元,则a的账户少100,b的账户多100,前后数据要一致: ( ...
- PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 8,DA=6,则 A 的“6 部分”PA 是 66,因为 A 中有 ...
- phpcms v9 标签调用,函数,sql
1.截取调用标题长度 {str_cut($r[title],36,'')} 2.格式化时间 调用格式化时间 2011-05-06 11:22:33 {date('Y-m-d H:i:s',$r[inp ...
- 实现Windows数据更新
一.枚举 枚举是一组描述性的名称 定义一组有限的值,不能包含方法 对可能的值进行约束 .定义枚举类 public enum Gender { Male,Female } .使用枚举表示整数值 publ ...
- NetCore使用使用Scaffold-DbContext命令生成数据库表实体类
一.为了模拟项目,本处创建了一个NetCore的Web项目.打算在Models文件夹下生成数据库表的实体类. 二.在程序包管理控制台,输入“Scaffold-DbContext "Serve ...