android 通知Notification详解及实例代码
发布时间 - 2026-01-10 22:05:52 点击率:次android Notification实例详解

1.使用Builder模式来创建
2.必须要设置一个smallIcon,还可以设置setTicker
3.可以设置 setContentTitle,setContentInfo,setContentText,setWhen
4.可以设置setDefaults(闪屏,声音,震动),通过Notification设置flags(能不能被清除)
5.发送需要获取一个NotificationManager(getSystemService来获取);notify(int id,Notification)
6.清除 manager.cancelAll(清除所有) cancal(int id); 如果需要16一下的api也能访问,需要导入v4包,使用notificationCompat兼容类。
自定义通知
使用RemoteViews来建立一个布局,然后使用setContent()设置;
点击事件使用PendingIntent来完成
下面是一个案例
MainActivity类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clearNotification(View v) {
// 通过代码来清除 NO_CLEAR
// 清除也需要manager
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 只能清除由本应用程序发出去的通知
manager.cancelAll();
}
public void sendNotification(View v) {
// 他是 在v4包中的通知,用于16以下版本发送通知
// NotificationCompat
// 通知的创建
Notification.Builder builder = new Notification.Builder(this);
// NotificationCompat.Builder builder2 = new NotificationCompat.Builder(
// this);
// 显示在通知条上的图标 必须的
builder.setSmallIcon(R.drawable.ic_launcher);
// 显示通知条上的文字 不是必须的
builder.setTicker("您有一条新的消息!!");
// 状态栏中的
builder.setContentTitle("大标题");
builder.setContentText("文本");
builder.setWhen(System.currentTimeMillis());
builder.setContentInfo("Info");
builder.setDefaults(Notification.DEFAULT_ALL);
// 点击事件使用Pending
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
// 生成对象 16API以上,支持低版本需要使用v4包中的notificationCompat
Notification notify = builder.build();
// 设置不能清除
notify.flags = Notification.FLAG_NO_CLEAR;
// 如何将通知发送出去
NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 通知的唯一值,如果id重复,表明是更新一条通知,而不是新建
mananger.notify((int) (Math.random() * 1000), notify);
}
public void diyNotification(View v) {
// 展示在通知上面的视图
RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setTicker("自定义通知 ")
// 布局
.setContent(views).build();
// 使用RemoteViews来设置点击事件
views.setTextColor(R.id.tv, Color.RED);
Intent intent = new Intent(this, OneActivity.class);
// 音乐播放是放在Service
PendingIntent pi = PendingIntent.getActivity(this, 2, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.tv, pi);
Intent intent2 = new Intent(this, MainActivity.class);
PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.iv, pi2);
// 发送
NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notify.notify(1, notification);
}
}
OneActivity类
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("跳转界面");
setContentView(tv);
}
}
activity.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lesson8_notification.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="普通的通知" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearNotification"
android:text="清除所有通知" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="diyNotification"
android:text="自定义通知" />
</LinearLayout>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
最后记得注册activity
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# android
# Notification
# Notification详解
# Notification实例代码
# Android使用Notification实现通知功能
# Android开发之Notification手机状态栏通知用法实例分析
# Android使用Notification在状态栏上显示通知
# Android中Notification通知用法详解
# Android 中Notification弹出通知实现代码
# Android中使用Notification实现状态栏的通知
# Android开发之Notification通知用法详解
# Android中通知Notification的使用方法
# Android Notification通知使用详解
# 自定义
# 包中
# 是一个
# 放在
# 还可以
# 他是
# 也能
# 希望能
# 跳转
# 谢谢大家
# 建立一个
# 来完成
# 如何将
# 栏中
# 应用程序
# 音乐播放
# 而不是
# 必须要
# public
# extends
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】
想要更高端的建设网站,这些原则一定要坚持!
Laravel的契約(Contracts)是什么_深入理解Laravel Contracts与依赖倒置
海南网站制作公司有哪些,海口网是哪家的?
Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】
详解jQuery停止动画——stop()方法的使用
长沙做网站要多少钱,长沙国安网络怎么样?
Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出
Laravel如何使用Service Provider注册服务_Laravel服务提供者配置与加载
javascript中的try catch异常捕获机制用法分析
大学网站设计制作软件有哪些,如何将网站制作成自己app?
如何在阿里云高效完成企业建站全流程?
Laravel Artisan命令怎么自定义_创建自己的Laravel命令行工具完全指南
Laravel如何配置.env文件管理环境变量_Laravel环境变量使用与安全管理
Linux系统命令中screen命令详解
如何快速搭建支持数据库操作的智能建站平台?
如何在云主机上快速搭建网站?
如何用免费手机建站系统零基础打造专业网站?
Claude怎样写约束型提示词_Claude约束提示词写法【教程】
jimdo怎样用html5做选项卡_jimdo选项卡html5实现与切换效果【指南】
北京网页设计制作网站有哪些,继续教育自动播放怎么设置?
企业网站制作这些问题要关注
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
如何在阿里云部署织梦网站?
如何为不同团队 ID 动态生成多个独立按钮
Android自定义控件实现温度旋转按钮效果
Laravel路由怎么定义_Laravel核心路由系统完全入门指南
如何在HTML表单中获取用户输入并用JavaScript动态控制复利计算循环
Laravel策略(Policy)如何控制权限_Laravel Gates与Policies实现用户授权
如何快速搭建安全的FTP站点?
深圳网站制作的公司有哪些,dido官方网站?
如何在阿里云服务器自主搭建网站?
Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】
Internet Explorer官网直接进入 IE浏览器在线体验版网址
Laravel如何处理CORS跨域请求?(配置示例)
Laravel如何实现文件上传和存储?(本地与S3配置)
Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】
如何在阿里云购买域名并搭建网站?
Python结构化数据采集_字段抽取解析【教程】
,南京靠谱的征婚网站?
详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
网站制作企业,网站的banner和导航栏是指什么?
Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件
Laravel的辅助函数有哪些_Laravel常用Helpers函数提高开发效率
微信小程序 HTTPS报错整理常见问题及解决方案
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口
油猴 教程,油猴搜脚本为什么会网页无法显示?
如何在 Telegram Web View(iOS)中防止键盘遮挡底部输入框
Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面
如何快速完成中国万网建站详细流程?

