当前位置:首页 > 芯闻号 > 充电吧
[导读]public class Notification extends Objectimplements Parcelable java.lang.Object    ↳ android.app.N

public class Notification extends Object
implements Parcelable java.lang.Object    ↳ android.app.Notification Class Overview

A class that represents how a persistent notification is to be presented to the user using theNotificationManager.

The Notification.Builder has been added to make it easier to construct Notifications.

1、创建NotificationManager对Notification进行管理:


NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

2、创建Notification:


Notification mNotification = new Notification.Builder(getApplicationContext())
		.setContentTitle("notification")
		.setContentText("content")
		.setSmallIcon(R.drawable.ic_launcher)
		.setLargeIcon(mBitmap)
		.setContentIntent(mPendingIntent)
		.setDefaults(Notification.DEFAULT_ALL)
		.build();

3、用NotificationManager的notify()方法将通知显示:


//void android.app.NotificationManager.notify(int id, Notification notification)
//id: An identifier for this notification unique within your application.
	mNotificationManager.notify(2,mNotification);


此时Notification还不能响应点击。

接下来用PendingIntent实现点击:


Intent intent = new Intent(this,NotificationActivity.class);
//PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

用Notification.Builder.setContentIntent():

Notification mNotification = new Notification.Builder(getApplicationContext())
		.setContentIntent(mPendingIntent)

部分关键代码:


//getResources()在onCreate()里,否则报空指针context为空
		//Resources android.content.Context.getResources()
		Intent intent = new Intent(this,NotificationActivity.class);
		//PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
		PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		//将qq.jpg转化为Bitmap,使用它设置大图标
		Bitmap mBitmap=BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.qq);
		NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
		
		Notification mNotification = new Notification.Builder(getApplicationContext())
		.setContentTitle("notification")
		.setContentText("content")
		.setSmallIcon(R.drawable.ic_launcher)
		.setLargeIcon(mBitmap)
		.setContentIntent(mPendingIntent)
		.setDefaults(Notification.DEFAULT_ALL)
		.build();
		/*int DEFAULT_ALL Use all default values (where applicable). 
		int DEFAULT_LIGHTS Use the default notification lights. 
		int DEFAULT_SOUND Use the default notification sound. 
		int DEFAULT_VIBRATE Use the default notification vibrate. 
		 */
		/*setLights(int argb, int onMs, int offMs) 
Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds).
		setSound(Uri sound) 
Set the sound to play.
setVibrate(long[] pattern) 
Set the vibration pattern to use.
long[] vibrates={0,1000,1000,1000} notification到来时震动1s停止1s再震动1s
vibrates[0]:静止时长
vibrates[1]:振动时长
vibrates[2]:静止时长
.... */
		//void android.app.NotificationManager.notify(int id, Notification notification)
		//id: An identifier for this notification unique within your application.
		mNotificationManager.notify(2,mNotification);		

将qq.jpg转化为Bitmap


Bitmap mBitmap=BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.qq);

还可以设置Notification到来时的振动,声音,灯光效果
Notification.Builder setLights(int argb, int onMs, int offMs)Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds). Notification.Builder setSound(Uri sound)Set the sound to play. Notification.Builder setVibrate(long[] pattern)Set the vibration pattern to use. 或者:


Notification.Builder setDefaults(int defaults)Set which notification properties will be inherited from system defaults.

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

近日,2024中关村论坛年会发布了10项重大科技成果名单,其中“转角氮化硼光学晶体原创理论与材料”备受关注。

关键字: 激光

负责支持和保护网络生活的云服务提供商阿卡迈技术公司(Akamai),近日在其日益丰富的云产品阵容中又增添了一款基于NVIDIA GPU的媒体优化型产品。这款全新的云服务产品基于NVIDIA RTX 4000 Ada Ge...

关键字: 视频解码器

业内消息,近日高通公司宣布推出针对桌面平台的全新骁龙 X Plus 处理器。

关键字: 高通 骁龙 X Plus 处理器

近日,台积电在圣克拉拉年度技术研讨会上宣布首个“埃级”制程技术:A16。A16 是台积电首次引入背面电源输送网络技术,计划于 2026 年下半年开始量产。同时,台积电也在重新命名工艺节点,标志着「埃级」时代的开始。

关键字: 台积电 A16

4 月 25 日消息,4 月 25 日,国际数据公司(IDC)发布 2024 年第一季度中国手机市场跟踪报告,荣耀以 17.1% 的市场份额拿下第一,华为占 17.0% 位列第二,OPPO、苹果和 vivo 分别位列第三...

关键字: 荣耀 华为

业内消息, 近日华为全新Pura 70系列手机正式开售引发广大 数码爱好者追捧,但是有网友注意到这款手机的“AI修图”功能,竟然可以将照片中的人物衣服消除,并拍成视频发布网络。

关键字: 华为Pura70 华为

据韩媒报道,近日韩国多位军方人士透露,韩国军方正在考虑全面禁止在军事建筑内使用苹果手机,军方担心敏感信息通过录音泄露。

关键字: iPhone 苹果

为了满足日益增长的数据处理需求,铁威马NAS推出了全新的性能巅峰2024年旗舰之作F4-424 Pro,并搭载了最新的操作系统--TOS 6。这款高效办公神器的问世,无疑将为企业和专业人士带来前所未有的便捷与效率。

关键字: 存储 Linux 服务器

继“特斯拉开启万人大裁员”之后,如今又一家车企扛不住了!

关键字: 电动汽车

轻量级AI定制模型助力低成本快速部署 北京2024年4月18日 /美通社/ -- 数据和人工智能(AI)领域的领先者SAS—今日在SAS Innovate大会上首次展示了一款变革性的、旨在帮助企业直面业务挑战的解决方案...

关键字: SAS AI模型 人工智能 INNOVATE
关闭
关闭