(1)创建一个NotificationManager对象 可通过Context的getSystemService()方法获取到,传入Context.NOTIFICATION_SERVICE.
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
(2)创建Notification对象。高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。
notification = new Notification.Builder(context).setAutoCancel(true)
.setContentTitle("qq正在运行").setContentText("qq,让交流更多方便")
.setContentIntent(mPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis()).build();
(3)调用NotificationManager的notify()方法让通知显示
manager.notify(id , 创建的Notification对象);
效果