Android开发

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

本文主要是介绍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);

这篇关于android中创建通知栏Notification代码实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!