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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-21 我要评论
想了解android中创建通知栏Notification代码实例的相关内容吗,在本文为您仔细讲解android创建Notification的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,通知栏,Notification,下面大家一起来学习吧。
///// 第一步:获取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);

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们