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 ...
随机推荐
- PMP--1.1 PMBOK体系框架描述
PMBOK:Project Management Body Of Knowledge(项目管理知识体系) <PMBOK>中收录被普遍认可的良好实践. 普遍认可:在大部分时候适用于大多数项目 ...
- 有关css编写文字动态下划线
<div class="main_text">哈哈这就是我的小视频</div> 上面为html代码 接下来进行css的编写 .main_text{ posi ...
- P5016 龙虎斗
链接:P5016 ------------------------------------ 作为2019年的模拟,还是有必要写一些的 --------------------------------- ...
- #AcWing系列课程Level-2笔记——4. 浮点数二分算法
浮点数二分算法 编写浮点数二分,记住下面的思路,代码也就游刃有余了! 1.首先找到数组的中间值,mid=(left+right)>>1,区间[left, right]被划分成[left, ...
- mysql升级后出现Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
安装了mysql5.7,用group by 查询时抛出如下异常: Expression #3 of SELECT list is not in GROUP BY clause and contains ...
- Mybatis Dao接口与Xml文件不匹配的问题:Invalid bound statement (not found)
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --> <resources> <resource> <directory&g ...
- PHP0009:PHP基础-mysql
以管理员省份启动记事本 修改host文件 插入外部sql数据
- MySQL 8 模式匹配
MySQL 提供了两种模式匹配: 标准 SQL 模式匹配 基于扩展正则的模式匹配 SQL 模式匹配使用 LIKE 命令进行模式匹配操作, _ 匹配任意单个字符.% 匹配任意多个字符,包含 0 个字符 ...
- WinForm WebBrowser 设置IE版本
public enum IeVersion { IE7 = , IE8 = , IE9 = , IE10 = , IE11 = }; /// <summary> /// 修改注册表信息来兼 ...
- C# compare different Encoding pattern between UTF8 and UTF32 based on Md5
using System; using System.Text; using System.IO; using System.Security.Cryptography; static void Ma ...