android创建通知栏,android 创建通知栏Notification(示例代码)
///// 第一步:获取NotificationManagerNotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);///// 第二步:定义NotificationIntent intent = new Intent(this, OtherActivity.class
/ 第一步:获取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);
更复杂的功能能够查询相关api文档。
更多推荐
所有评论(0)