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

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

纳祥科技推出太阳能+Type-C双充电自行车前灯方案,方案核心模块包含太阳能板、单片机、三极管、3颗LED灯珠与1200mAh电池,通过低功耗单片机与三极管驱动,支持强光/弱光/爆闪3种模式,高流明远射程,适配多种车型

关键字: 方案开发 电子方案 自行车前灯方案 纳祥科技

慕尼黑2025年9月11日 /美通社/ -- 当地时间9月9日,赛力斯动力在德国慕尼黑国际车展期间举办技术发布与交流会,正式在海外市场推出全新一代赛力斯超级增程、高效发动机和新一代分布式电驱动系统,同时与来自全球的汽车产...

关键字: 慕尼黑 分布式 发动机 新能源汽车

慕尼黑2025年9月11日 /美通社/ -- 高端智能电动汽车品牌问界(AITO)在2025年德国国际汽车及智慧出行博览会(IAA MOBILITY)上,正式发布了其最新全球产品阵容——专为中东市场深度本地化打造的AIT...

关键字: AI 智能驾驶 测试 生态系统

舍弗勒首次为中国头部车企大规模生产高压逆变砖 天津工厂一年内完成量产准备,逆变器模块性能参数显著提升 与合作伙伴罗姆半导体共研尖端碳化硅技术,效率更高、性能更优 模块化可扩展设计使逆变砖易于集成,可广泛...

关键字: 逆变 高压 逆变器 集成

舍弗勒以"专注驱动技术的科技公司"为主题亮相IAA MOBILITY 2025(B3馆B40展台) 合并纬湃科技后首次亮相IAA MOBILITY,展示拓展后的汽车产品组合 凭借在软件、...

关键字: 电气 软件 驱动技术 BSP

拉斯维加斯2025年9月11日 /美通社/ -- 在9月8日至11日举办的RE+ 2025展会上,全球综合储能解决方案供应商德赛电池(Desay Battery)全面展示了其创新成果,并宣布与深圳市华宝新能源股份有限公司...

关键字: 电池 电芯 人工智能 锂电

香港2025年 9月12日 /美通社/ -- 全球领先的互联网社区创建者 - 网龙网络控股有限公司 ("网龙"或"本公司",香港交易所股票代码:777)欣然宣布,其子公司My...

关键字: AI 远程控制 控制技术 BSP

慕尼黑2025年9月12日 /美通社/ -- 慕尼黑当地时间9月10日,在2025德国国际汽车及智慧出行博览会(IAA MOBILITY)上,国际独立第三方检测、检验和认证机...

关键字: 测试 慕尼黑 模型 HUBER

上海2025年9月12日 /美通社/ -- 近日,国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(简称"TÜV莱茵")为上海...

关键字: 测试 信息安全 安全管理 开关

广州2025年9月12日 /美通社/ -- 9月11日,由国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(简称"TÜV莱茵"...

关键字: 数字化 供应链 控制 电子
关闭