Android Notification 基础(一)

本文深入解析了Android中Notification的基本概念、实现原理及关键代码解释,通过实例代码演示了如何在Android应用中创建并展示通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android中,基本的Notification就是有事件发生的时候在屏幕顶端的Notificat<wbr>ion bar上显示一个图标。然后拉下Notification bar,点击Notification的项目,会调用相应的程序做处理。比如有新短信,就会出现短信的图标<wbr>,拉下Notification bar,点击图标会调用短信查看程序。</wbr></wbr>

我们先看一下Notification的Sample Code,然后逐行做解说,大致能了解它的基本构成。

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

...

private void showNotification(Message msg, int id) {
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.notiicon, msg
				.getTitle(), System.currentTimeMillis());
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		Intent intent = new Intent(this, MainActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("info", msg.getInfo());
		intent.putExtras(bundle);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent contentIntent = PendingIntent.getActivity(this, id,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

		notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
				contentIntent);
		notiManager.notify(id, notification);
	}

首先导入三个类,Notification,NotificationManager,PendingIn<wbr>tent。 值得一提的是PendingIntent,它可以看做是Intent这封信的一个信封。PendingIn<wbr>tent基本上是Intent的包装和描述,对象收到PendingIntent后,可以得到其中的Int<wbr>ent再发出去。</wbr></wbr></wbr>

NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

上面这一句,从系统中获得Notification服务,getSystemService()就是这么用<wbr>,来获得系统服务的。</wbr>

Notification notification = new Notification(R.drawable.notiicon, msg
				.getTitle(), System.currentTimeMillis());
		notification.flags = Notification.FLAG_AUTO_CANCEL;

然后是构造一个Notification,包括三个属性,图标,图标后面的文字,以及Notificati<wbr>on时间部分显示出来的时间,通常使用当前时间。FLAG_AUTO_CANCEL说明Notificat<wbr>ion点击一次就消失。</wbr></wbr>

Intent intent = new Intent(this, MainActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("info", msg.getInfo());
		intent.putExtras(bundle);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);

上面这部分Code,构造一个Intent,并且放进去一条信息。 FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK者两个FLAG表示优先寻找已经打开的应用,如果应用没有打<wbr>开那么启动它。</wbr>

PendingIntent contentIntent = PendingIntent.getActivity(this, id,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

这一句代码把Intent包装在PendingIntent里,this是Context,id是Pend<wbr>ingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来<wbr>的PendingIntent会更新前面的。</wbr></wbr>

notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
				contentIntent);
		notiManager.notify(id, notification);

最后这两行添加状态栏的详细信息,包装PendingIntent给Notification,最后发送N<wbr>otification。</wbr>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值