Notification使用详解之一:基础应用

在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下Notification具体的使用方法。

首先说明一下我们需要实现的功能是:在程序启动时,发出一个通知,这个通知在软件运行过程中一直存在,相当于qq的托盘一样;然后再演示一下普通的通知和自定义视图通知。

那我们就先建立一个名为notification的项目,然后编辑/res/layout/main.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="点击按钮进入演示界面"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="notify activity" android:onClick="notify"/> </LinearLayout>

然后编辑MainActivity.java文件,代码如下:

package com.scott.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private static final int ONGOING_ID = 0; private NotificationManager mNotificationManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setUpNotification(); } private void setUpNotification() { Context context = this; mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.ongoing; CharSequence tickerText = "程序已启动"; long when = System.currentTimeMillis(); //新建一个Notification实例 Notification notification = new Notification(icon, tickerText, when); // 把通知放置在"正在运行"栏目中 notification.flags |= Notification.FLAG_ONGOING_EVENT; CharSequence contentTitle = "Notification示例"; CharSequence contentText = "程序正在运行中,点击此处跳到演示界面"; Intent intent = new Intent(context, NotifyActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify(ONGOING_ID, notification); } public void notify(View view) { Intent intent = new Intent(this, NotifyActivity.class); startActivity(intent); } @Override public void onBackPressed() { super.onBackPressed(); finish(); //取消一个通知 mNotificationManager.cancel(ONGOING_ID); //结束进程 android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } }

大家看以看到,在程序主界面启动时会发出一个通知,并且将这个通知放置在“正在运行”栏目中,这样在软件运行过程中它就会始终存在;另外,在上面的代码中,在用户按下回退按钮时,我们使用NotificationManager.cancel(int id)方法取消这个通知。

先来看一下运行效果如何:

点击主界面的按钮和Ongoing栏目中的通知均能跳转到NotifyActivity界面,在这个界面中,我们主要演示一下普通通知和自定义视图通知的使用。

我们先要在/res/layout/目录下添加一个notify.xml布局文件,代码如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="normal notify" android:onClick="normalNotify"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="custom notify" android:onClick="customNotify"/> </LinearLayout>

因为要演示自定义通知视图,我们需要定义一个自定义通知视图的布局文件,摆放我们自己的布局组件,因此在/res/layout/目录下添加一个custom_notification_layout.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dp"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#000"/> </LinearLayout>

然后就来看一下NotifyActivity.java的代码:

package com.scott.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.RemoteViews; public class NotifyActivity extends Activity { //注意,如果不想覆盖前一个通知,需设置不同的ID private static final int NORMAL_NOTIFY_ID = 1; private static final int CUSTOM_NOTIFY_ID = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notify); } // 普通通知事件 public void normalNotify(View view) { Context context = this; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = android.R.drawable.stat_notify_voicemail; CharSequence tickerText = "普通通知"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); // 设定声音 notification.defaults |= Notification.DEFAULT_SOUND; //设定震动(需加VIBRATE权限) notification.defaults |= Notification.DEFAULT_VIBRATE; // 设定LED灯提醒 notification.defaults |= Notification.DEFAULT_LIGHTS; // 设置点击此通知后自动清除 notification.flags |= Notification.FLAG_AUTO_CANCEL; CharSequence contentTitle = "普通通知的标题"; CharSequence contentText = "通知的内容部分,一段长长的文字..."; Intent intent = new Intent(context, TargetActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify(NORMAL_NOTIFY_ID, notification); } // 个性化通知点击事件 public void customNotify(View view) { Context context = this; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = android.R.drawable.stat_notify_voicemail; CharSequence tickerText = "自定义通知"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); notification.flags |= Notification.FLAG_AUTO_CANCEL; RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout); contentView.setImageViewResource(R.id.imageView, R.drawable.smile); contentView.setTextViewText(R.id.textView, "这是一个个性化的通知视图,代替了系统默认的通知视图."); // 指定个性化视图 notification.contentView = contentView; Intent intent = new Intent(context, TargetActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); // 指定内容意图 notification.contentIntent = contentIntent; mNotificationManager.notify(CUSTOM_NOTIFY_ID, notification); } }

注意,上边在添加一个普通通知时使用到了震动,所以需要在AndroidManifest.xml中加入相关权限:

<uses-permission android:name="android.permission.VIBRATE"/>

除了使用代码中的默认通知属性之外,用户也可以自定义属性值:

1.自定义声音:

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

2.自定义震动方式:

long[] vibrate = {0, 100, 200, 300}; notification.vibrate = vibrate;

这个数组定义了交替的震动和关闭,以毫秒为单位。第一个值是等待多久开始震动,第二个值是第一次震动的时间,第三个值是停止震动的时间,以此类推。定义多长时间都行,但是不能设置为重复。

3.自定义闪光方式:

notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;

上边几行代码表示绿灯先显示300毫秒然后关闭一秒钟。如果设备不支持指定的颜色,则会按照最接近的颜色显示。

如果全部都使用默认值时,可以用以下代码代替程序中的几行设定defaults的代码:

notification.defaults |= Notification.DEFAULT_ALL;

注意,在自定义以上属性时,如果defaults中与之相关的默认值已设置,则自定义属性就会失效。

然后再来介绍一下几种常用的flags:

1.FLAG_AUTO_CANCEL:在用户查看通知信息后自动关闭通知;

2.FLAG_INSISTENT:在用户响应之前一直重复;

3.FLAG_ONGOING_EVENT:放置在“正在运行”栏目中,表示程序正在运行,可见状态,或是后台运行;

4.FLAG_NO_CLEAR:查看通知后不会自动取消,对一直进行中的通知非常有用。

在上面的程序中,点击通知后跳转到TargetActivity界面,这个界面非常简单,就显示一串文字,这里就不必多说了。

最后让我们演示一下效果:

关于notification的相关知识,今天先介绍到这里,以后会继续介绍更深入的使用技巧。
Android中new Notification创建实例的最佳方法 主要介绍了Android中new Notification创建实例的最佳方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 立即下载

相关推荐

Android开发 — 状态栏通知Notification、NotificationManager详解

本想自己写一个的,但是看到这篇之后,我想还是转过来吧,实在是非常的详细: 在Android系统中,发一个状态栏通知还是很方便的。下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationManager 、 Notification。 NotificationManager :  是状态栏通知的管理类,负责发通知、清楚通知等。 NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。 代码如下:NotificationManager nm =

HTML5 桌面通知 new Notification

1.背景: 房产咨询,用户和房博士聊天,当发送聊天消息时,需要做收到消息通知,最后查到HTML5新增的 Notification API的通知功能。即使将浏览器窗口最小化,依然会收到消息通知。 2.通知权限: 2.1 首先需要 看浏览器是否支持 Notification ,支持才有下文,否则就此止步; 2.2 通过 Notification.permission 检测用户是否允许通知: // Notification.permission === 'granted' 用户允许 // ...

hangGe0111的博客 2589

Carsim和Matlab/Simulink联合仿真时Carsim匹配不到Matlab的解决方法

      本人使用Carsim和Matlab/Simulink联合仿真做过用机器学习方法解决ACC和简单换道的仿真,曾将8.02 2016 2018三个版本Carsim反复安装N次,有相同方向的同学或安装过程遇到问题的同学可以加本人qq505625869一起交流经验心得。 一.前言       Carsim是一款很好的联合仿真软件,使用Carsim,可以避免搭建很多非常复杂的动力学模型

Kaikai_Liao的博客 8773

在vue中如何使用: new Notification

注意:由于通知相关的API设计有限制,通常只能在用户主动交互(例如点击按钮)的事件处理程序中请求授权和创建通知。在非用户主动交互的情况下尝试创建通知(例如在组件的生命周期钩子中)可能会被浏览器阻止。用户授权:为了向用户显示通知,需要获得用户的授权。API在现代浏览器中是可用的,但部分浏览器可能存在兼容性问题。来创建通知,传递通知的标题和选项。根据用户的授权情况,将决定是否创建通知。请参考浏览器的相关文档以及适当处理和尊重给用户的通知权限和体验。在Vue中,你可以在组件的方法中调用。在Vue中,你可以使用。

蓝斑.json的博客 1138

薄膜比拟、开口截面扭转承载力以及风电混塔水平缝张开状态下抗扭承载力推导

风电混塔水平缝张开状态下的抗扭承载力推导,从弹性力学说起

George的博客 881

告别卡顿!OBS Studio最佳录制参数设置指南(适合游戏/网课/会议场景)

本文提供OBS Studio录制参数优化指南,针对游戏、网课及会议三大场景,详细解析帧率、码率与编码器设置。重点推荐利用硬件编码器(如NVENC)实现游戏录制零卡顿,并分享清晰稳定的网课录制与长时间会议录制配置方案,助你告别视频卡顿与音画不同步问题。

e4f5g6h7的博客 1345

Android Notification 使用详解之一:基础应用

前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net          雨季o莫忧离:http://blog.csdn.net/luckkof 正文   在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长

奥特曼 1272

Computer Networks 和 Computer & Security投稿经验

论文投稿

qq_50604294的博客 9260

Notification---Notification使用详解之一:基础应用

在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下Notification具体的使用方法。 首先说明一下我们

yyb.ye专栏 1096

半导体材料仿真:二维材料仿真_(1).二维材料的基本概念

在半导体材料仿真领域,二维材料因其独特的物理和化学性质而受到广泛关注。二维材料,也称为二维纳米材料,是指在二维空间(通常是平面)上具有原子级厚度的材料。这些材料的厚度通常在几个纳米甚至单个原子层的范围内,使得它们在电子、光学、热学和机械性能上表现出与传统三维材料显著不同的特性。本节将详细介绍二维材料的基本概念,包括它们的分类、结构、性质以及在半导体材料仿真中的应用。

2401_87715305的博客 1549

科研绘图系列:R语言折线图(linechart plots)

科研绘图系列:R语言折线图(linechart plots)

专注生信领域 1277

(转)Notification使用详解之一:基础应用

在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下Notification具体的使用方法。 首先说明一下

gxj1680的专栏 737

IgH EtherCAT 从入门到精通:第 30 章 实战:高可用 EtherCAT 系统设计

摘要:构建高可用EtherCAT系统的关键技术 本章详细介绍了工业级EtherCAT系统的高可用性设计方法,主要包括: 冗余链路架构:采用双网卡环形拓扑设计,主备网卡同时工作,通过IgH Master的冗余配置实现线缆故障自动切换。 状态监控机制:提供专用API实时监控主备链路状态,包括从站响应数、链路状态和冗余激活标志,形成完整的故障判断矩阵。 自动恢复功能:系统内置FSM架构支持热插拔自动检测和重配置,从站断线/重连时可自动完成状态恢复,同时允许应用层实施安全措施。 这些技术共同构成了生产环境下Ethe

从事嵌入式linux android驱动开发,拥有全栈能力 249

AT-UI消息通知系统:Alert、Message、Notification组件详解

AT-UI是一个基于Vue.js 2.0开发的桌面端UI组件库,提供了丰富的消息通知解决方案。本文将详细介绍其三大核心通知组件——Alert警告提示、Message全局提示和Notification通知提醒,帮助开发者快速掌握这些组件的使用方法和最佳实践。 ## AT-UI通知组件概述 📋 AT-UI的消息通知系统设计简洁直观,针对不同场景提供了三种各具特色的通知组件: - **Alert

gitblog_00608的博客 877

R语言机器学习算法实战系列(十五)随机森林生存预后模型+SHAP值 (Random Survival Forest + SHAP)

本教程运用随机森林生存分析筛选与患者生存状态显著相关特征。该方法借随机森林这一强大机器学习算法,构建多棵决策树并集成结果,以提高生存分析准确性,识别关键生物标志物。接着,利用选定特征构建风险得分,将多个特征转化为单一数值,量化患者风险水平。之后,依据风险得分将患者分为高、低风险亚型,为临床治疗提供个性化指导。最后,通过分析时间依赖的 ROC 曲线评估模型长期预测能力。此曲线计算不同阈值下的真正例率与假正例率,在多个时间点评估,全面展现模型长期稳定性与准确性。。并且增加了SHAP值评估特征重要性

专注生信领域 2637

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

上一篇: Android中处理崩溃异常
下一篇: 在代码中引入Null Object模式
iteye_7884
博客等级 码龄8年 44粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值