Android 通知Notification

Android 通知栏Notification的简但使用,效果如上图,废话不说,代码:

 1 private void notification() {
 2         Notification notification;
 3         int notification_id = 11;
 4 
 5         //通过系统服务来获取对象
 6         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 7         //创建一个通知对象
 8         Notification.Builder nbuider = new Notification.Builder(MainActivity.this);
 9         //设置Notification的效果为默认效果
10         nbuider.setDefaults(Notification.DEFAULT_LIGHTS);
11         //设置Notification 第一次出现在状态拦下的文本
12         nbuider.setTicker("通知你去出差!");
13         //标题
14         nbuider.setContentTitle("通知");
15         //小标题
16         nbuider.setContentTitle("就是了");
17         //小标题
18         nbuider.setContentText("好啊");
19         //延迟0ms,然后振动500ms,延迟300ms,接着振动350ms。
20         nbuider.setVibrate(new long[]{0,500,300,350});
21         nbuider.setSmallIcon(R.drawable.e);//不设置图标出不来,不知什么情况
22 
23         //创建Intent对像,作为pendingIntent 参数
24         Intent intent = new Intent(MainActivity.this,AnimationActivity.class);
25         //Activity与pendingIntent关联的对象
26         PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
27 
28 
29         //打开触发
30         nbuider.setContentIntent(pi);
31 
32         //创建通知
33         notification =nbuider.build();//API 16以上才能使用
34         notification.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知后原来文本图标消失
35         //发送通知
36         notificationManager.notify(notification_id,notification);
37     }

 

posted @ 2015-07-31 10:58  黄海-1991  阅读(150)  评论(0编辑  收藏  举报