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

深入解析Notification和NotificationManager:编程实现与管理 在上述代码中,首先创建了一个NotificationCompat.Builder对象,通过该对象设置通知的各种属性,如图标、标题、内容和优先级等。在上述代码中,首先创建了一个NotificationCompat.Builder对象,通过该对象设置通知的各种属性,例如图标、标题、内容和优先级等。它提供了一系列方法,用于创建、更新和取消通知,并可以设置通知的优先级、声音、震动等属性。它提供了一系列方法,用于创建、更新和取消通知,并可以设置通知的优先级、声音、震动等属性。一、通知(Notification) 阅读详情

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN:http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

 

在消息通知时,我们经常用到两个组件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常驻通知栏Notification的完整指南 Android Notification是Android系统中用于向用户显示应用状态更新的一种机制。它能够让应用在不打断用户当前操作的情况下,展示信息。随着Android版本的更新,Notification的管理和用户交互方式也发生了很大的变化。创建一个Notification Channel,首先需要定义一个Channel ID和Channel Name。Channel ID是一个字符串,用于在程序中唯一标识这个Channel。Channel Name是用户可见的名称,应简洁明了地描述通知的用途。 阅读详情

相关推荐

Android Notification.Builder通知构建实战教程

在本章中,我们将简要介绍Android Notification的核心概念,以及它在移动应用开发中的重要性。Android Notification是Android系统中用于向用户展示信息的一种重要方式,它允许应用在不需要用户进入应用界面的情况下通知用户有关信息。从简单的文本消息到复杂的富媒体内容, Notifications提供了一个灵活的渠道来增强用户体验,是提升用户参与度的关键工具。

weixin_27645199的博客 1117

HTML5 桌面通知 new Notification

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

hangGe0111的博客 2589

Android中new Notification创建实例的最佳方法

主要介绍了Android中new Notification创建实例的最佳方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

在vue中如何使用: new Notification

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

蓝斑.json的博客 1138

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

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

matlab tolx,Nelder-Mead算法matlab

Nelder-Mead算法适合变量数不是很多的方程求极值.原理:Nelder-Mead法是利用多面体来逐步逼近最佳点x*.设函数变量为n维,则在n维空间里多面体有(n+1)个顶点.设x1,x2,...,xn+1为多面体的顶点,且满足:f(x1)<=f(x2)<=...<=f(xn+1)Nelder-Mead法试着将多面体中最差的顶点xn+1(也就是函数的最大点)以新的最佳点替代,...

weixin_36485298的博客 1483

Android 基础 notification 简单应用

 Notification:系统通知 用系统的布局来提示 步骤: 首先创建一个类(接收器)MyReceiver继承BroadcastReceiver重写onReceive方法在方法里   需要得到通知的管理(NotificationManger) NotificationManger manager=(NotificationManger)context.

Applicaton的专栏 872

从‘数字分身’到‘数字员工’:华为云MetaStudio如何重塑企业生产力?

本文探讨了华为云MetaStudio如何通过数字人技术重塑企业生产力,从数字分身到数字员工的转变。通过多模态AIGC技术,MetaStudio在金融、零售和制造业等多个行业实现高效应用,显著提升服务质量和运营效率。数字员工不仅降低成本,更成为企业增长的新引擎。

weixin_29083475的博客 456

Android手机应用开发(四) | Broadcast使用和Notification、EventBus编程基础

实验目的 掌握 Broadcast 编程基础。 掌握动态注册 Broadcast 和静态注册 Broadcast。 掌握Notification 编程基础。 掌握 EventBus 编程基础。 Broadcast使用 BroadcastReceiver(广播接收器),属于 Android 四大组件之一 注册的方式分为两种:静态注册、动态注册 静态注册 1.注册广播 创建一个java类St...

Janking's Blog 1124

麒麟V10等保三级配置实战:从auditd规则到PAM锁定的可信基线构建

Linux安全基线是信息系统可信运行的基础能力,其核心在于身份鉴别、访问控制与安全审计三大机制的协同落地。在国产化信创环境中,麒麟V10作为深度适配飞腾/鲲鹏平台的操作系统,其等保三级合规并非简单套用CentOS加固脚本,而是需围绕内核审计(auditd)、可插拔认证模块(PAM)及国产安全增强机制(如kysec)进行精细化编排。技术价值体现在将抽象条款(如GB/T 22239-2019中6.1.2、6.3.3条)转化为可验证的命令行操作,确保登录失败可锁定、关键行为可追溯、时间偏差不致日志失效。典型应用场

weixin_29969209的博客 607

使用CAPL实现DoIP刷写

本文介绍了使用CAPL实现基于DoIP协议的ECU刷写方法,包含完整源码。

qq_56033302的博客 2773

Android基础学习总结(四)——Notification应用解析

引言 Notification可以让我们在获得消息的时候,在状态栏,锁屏界面来显示相应的信息,很难想象如果没有Notification,那我们的qq和微信以及其他应用没法主动通知我们,我们就需要时时的看手机来检查是否有新的信息和提醒着实让人烦心,也体现出Notification重要性。这里会介绍三种Notification,分别是普通的Notification,折叠式Notification和悬

. 340

FOC实战笔记:RC吸收电路的设计考量与参数调优

本文深入探讨了FOC驱动器中RC吸收电路的设计与调优。针对MOS管开关过程中的电压尖峰问题,详细阐述了RC电路的钳位与能量吸收原理,并提供了从参数计算、实验调试到布局布线的完整实战指南,旨在帮助工程师有效提升系统可靠性与稳定性。

weixin_29219539的博客 284

Android基础入门教程——2.5.2 Notification(状态栏通知)详解

本节带来的是Android中用于在状态栏显示通知信息的控件:Notification,相信大部分 学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2.x的,而 现在普遍的Android设备基本都在4.x以上,甚至是5.0以上的都有;他们各自的Notification 都是不一样的!而本节给大家讲解的是基于4.x以上的Notification,而5.0以上的Notifica

Coder-Pig的猪栏 3万+

42、多媒体与虚拟现实在艺术和影视领域的创新应用

本文探讨了多媒体与虚拟现实在艺术和影视领域的创新应用,涵盖三个典型案例:影视与人类学融合项目通过协作模式模糊作者与观众界限,推动文化文献的发展;交互式数字短片《分手》利用基于对象的广播技术探索个性化影视体验;威尼斯双年展瑞士馆4D项目则通过虚拟现实技术实现文化遗产的数字化展示。这些案例展示了技术在叙事、用户体验及文化保护中的深度应用,并展望了未来技术融合、个性化深化与文化遗产传承的发展趋势。

kappa的博客 117

全志T527 适配双目tp2815_mipi

在全志T527 linux下适配双目TP2815 mipi摄像头,支持六路同时输出,四路可用作360环视、一路face id等,一路用于其它开发。

Linux驱动开发工程师,熟悉Linux/Android底层驱动、TP、显示、IIC、网络等通用外设调试适配,Linux上层应用开发。常用平台瑞芯微、全志、恩智浦等。 3848

Notification通知

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } publi

LLLigj博客 403

【AUTOSAR-E2E】-2.3-配置E2E:基于E2EPW配置一个RX接收的Signal Group(使用E2E Profile 1)

《共13页精讲》:此法也适用于E2E Profile 4/5/6/7/11/22 目录 1 检查CAN Matrix Arxml/DBC是否满足E2E需求 2 在DaVinci Developer中为Port添加E2E保护 3 在DaVinci Configurator settings设置 4 如何使用应用层的E2E保护 5 结尾 <--返回「Autosar_BSW高阶配置」总目录--> <--返回「Autosar_BSW高阶配置」专栏主页--> <-.

「汽车电子助手」的博客 3957

【Android】状态栏通知Notification、NotificationManager详解

在Android系统中,发一个状态栏通知还是很方便的。下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置?   首先,发送一个状态栏通知必须用到两个类:  NotificationManager 、 Notification。   NotificationManager :  是状态栏通知的管理类,负责发通知、清楚通知等。 Notification

chuyouyinghe的专栏 363

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Notification_安卓

定义:一种可以显示即时信息的控件,该控件显示在标题栏,拉开后会看到通知的完整样式。 Notification和NotificationManager来处理状态栏的推送信息,使用它两发送显示通知分为四个步骤:     ①、调用(NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE)获取系统的No

Yo_Hack的专栏 550

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

安卓 Notification 使用方法小结

本文简单介绍安卓 Notification 的用法,并简单对比 5.0 前后 Notification 的变化和处理方法。

关注移动端、Web、微信小程序等大前端领域 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的

LiuHe 1万+

【安卓笔记】notification使用

Notification能够使你的应用程序能够在不使用Activity的情况下通知用户。虽然Notification与Toast都可以起到通知、提醒的作用。但它们的实现原理和表现形式却完全不一样。Toast其实相当于一个组件 (Widget)。有些类似于没有按钮的对话框。而Notification是显示在屏幕上方状态栏中的信息。还有就是Notification需要用 NotificationM

RowandJJ 2498

安卓Notification通知栏全解

全栈工程师开发手册 (作者:栾鹏) 安卓教程全解 安卓通知栏是提醒用户信息有效手段,也是通过用户的行为触发事件服务的方式。 Notification允许在当前应用程序不活动或不可见时向用户发送信号。 创建通知栏有使用Notification直接创建何使用Notification.Builder创建两种方法 1、使用Notification直接创建通知栏 private

1869

android 悬浮通知栏_(Android)Notification使用详解

Notification使用Notification是Android中当应用程序在后台运行时,借助通知就可以在手机最上方显示一个通知的图标,下拉状态栏就可以看到详细内容。基本概念通知还是比较灵活的,可以在活动中创建,也可以在服务和广播中创建,但是通知通常是在程序在后台运行时调用。总体来说,创建和使用用法都是相同的。详细步骤创建NotificationManager来对通知进行管理,通过调用getS...

weixin_32897341的博客 1795

android Notification详解

先介绍一下旧版的使用方式: 旧版使用的是Notification和M

lsyAndroid的专栏--解决实际开发中自己遇到的问题 882
上一篇: Android Manifest.xml详解
下一篇: Android Notification 使用详解之二:可更新进度的通知
野猪佩奇o
博客等级 码龄15年 253粉丝 129原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值