android中创建通知栏Notification代码实例
程序员文章站
2022-04-30 22:56:15
///// 第一步:获取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);