欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android中创建通知栏Notification代码实例

程序员文章站 2022-08-17 18:38:51
///// 第一步:获取notificationmanager notificationmanager nm = (notificationmanager)...
///// 第一步:获取notificationmanager
		notificationmanager nm = (notificationmanager) 
				getsystemservice(context.notification_service);

		///// 第二步:定义notification
		intent intent = new intent(this, otheractivity.class);
		//pendingintent是待执行的intent
		pendingintent pi = pendingintent.getactivity(this, 0, intent,
				pendingintent.flag_cancel_current);
		notification notification = new notification.builder(this)
				.setcontenttitle("title")
				.setcontenttext("text")
				.setsmallicon(r.drawable.ic_launcher).setcontentintent(pi)
				.build();
		notification.flags = notification.flag_no_clear;
		
		/////第三步:启动通知栏,第一个参数是一个通知的唯一标识
		nm.notify(0, notification);
		
		//关闭通知
		//nm.cancel(0);