Android开发UI之Notification

Notification,顾名思义,通知,就是我们常说的系统推送。

官网链接:http://developer.android.com/reference/android/app/Notification.html

要想使用Notification,还需要使用几个类,NotificationManagerNotificationCompat.BuilderPendingIntentIntent

这个是官网教你如何创建一个Notificiation:http://developer.android.com/training/notify-user/build-notification.html#action

根据官网的教程,练习创建一个Notification,并实现点击Notification跳转。

1.使用NotificationCompat.Builder类,创建一个Notification Builder。

NotificationCompat.Builder类位于support lib中的android.support.v4.app.NotificationCompat.Builder。继承自Object.

1 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)   //使用的是support.v4.app.NotificationCompat.Builder
2 mBuilder.setSmallIcon(R.drawable.notification_icon)                          //设置icon
3 mBuilder.setContentTitle("My notification")                                  //设置标题Title
4 mBuilder.setContentText("Hello World!");                                     //设置内容

2.设置点击Notification的动作

1 Intent resultIntent = new Intent(this, ResultActivity.class);                //使用Intent,设置跳转到ResultActivity
2 PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); //使用PendingIntent
3 mBuilder.setContentIntent(resultPendingIntent); //
1 Notification notification=mBuilder.build();  //获取真实的nofitication

3.发出通知

1 int mNotificationId = 001;                                                   //设置Nofitication的ID
2 NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //获取NotificationManager service实例
3 mNotifyMgr.notify(mNotificationId,notification); //构建一个Notification,并发出

 

以上就完成了一个Notication的显示和动作。

posted @ 2015-07-17 23:09  熠然  阅读(437)  评论(0编辑  收藏  举报