当前位置:首页 > 芯闻号 > 充电吧
[导读]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.

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

香港2026年2月11日 /美通社/ -- 全球领先的通用人工智能科技公司MiniMax Group Inc.("MiniMax"或"公司";香港联交所股票代码:00100)今日宣布,将于2026年3月2日(星期一)在香...

关键字: NI 电话会议 PS 人工智能

上海2026年2月11日 /美通社/ -- 近日,索迪斯于中国总部成功举办首届ExpInnova一起创新日。作为索迪斯在中国推出的全新沉浸式创新体验平台,本次活动以"未来可及•以创新雕琢体验"为主题,...

关键字: PI NOVA 可持续发展 DIY

深圳2026年2月11日 /美通社/ -- 今天,全球领先的以人工智能(AI)与机器人自动化实验技术驱动研发创新的平台型企业晶泰科技(2228.HK)宣布,已成功向韩国头部药...

关键字: AI SCIENCE 机器人 泰科

全新推出的Personalize隶属于Adyen Uplift,通过人工智能洞察购物者行为,动态调整结账体验,助力企业提升转化率,同时降低交易成本。 2025年,A...

关键字: PERSONAL AD TE AMS

新加坡2026年2月11日 /美通社/ -- AI 基础设施解决方案提供商 SuperX AI Technology Limited(纳斯达克股票代码:SUPX,以下简称"S...

关键字: SUPER 通信 AI BSP

沙特阿拉伯吉达2026年2月11日 /美通社/ -- ABB国际汽联电动方程式世界锦标赛第12赛季的首个双赛分站即将拉开帷幕,第4轮与第5轮比赛将于2026年2月13日至14日在标志性的吉达滨海赛道举行。 第十二赛季F...

关键字: 电动 快充 BSP EV

重庆2026年2月11日 /美通社/ -- 2026年是国家"十五五"规划的开局之年,也是成渝地区双城经济圈建设由"积厚成势"迈向"成型成势"的关键节点。为抢占...

关键字: 智能体 人工智能 模型 节点

- HUMAIN通过战略性收购ai.io的控股权,推出HUMAIN Sport,宣布将致力于推进AI驱动型体育技术的发展。 - 此次收购将加速AI与体育技术解决方案在沙特阿拉伯及全球市场的快速发展与落地。 - HUMAI...

关键字: AI SPORT IO 人工智能

凭借寿险AI理赔自动化与决策能力获评 纽约和新加坡2026年2月11日 /美通社/ -- Neutrinos作为保险行业AI驱动智能自动化领域的领导者宣布,公司已...

关键字: OS CE AN AUTOMATION

上海2026年2月11日 /美通社/ -- 1月23日,由Global ConsignIndex跨盈指数举办的第二十三届跨盈年度B2B营销高管峰会(CC2026)在上海落下帷幕。大会聚焦主题"The...

关键字: AI NI BSP GO
关闭