6.0之后Notification的基本用法(部分翻译自google官方文档)
2016-10-07 17:41
561 查看
1、creat a Notification Builder
一个Builder至少包含:
a small icon 用setSmallIcon来设置
a title 用setContentTitle来设置
detail text 由setContentText来设置
eg:
2、Define the notification‘s action
一个通知至少由一个action,action通常让用户通过通知跳转到一个应用的活动中,它们可以看作是产生通知或做更多工作的一个event,在通知中,action由包含与Intent中的pendingIntent来定义,而这个Intent通常在你应用的Acitivity中开始。
如何构造一个pendingIntent取决与你的Activity是什么类型的,当你从通知中开启一个活动时,你必须保存用户的导航体验(回到之前的活动界面),在下面代码中,单击通知打开一个新的活动,拓展了通知的功能,这种情况下就不会创建一个人工的回堆栈。
eg:
3、Set the notification's click behavior
为了将之前创建的pendingIntent和手势联系起来,用Notification.Builder中的合适的方法来实现。比如当用户点击一个通知栏的时候开启一个Activity,通过setContentIntent()方法将PendingIntent加入。
eg:
4、Issue the Notification
发通知
① 获得NotificationManager的实例
②用notify方法让通知显示出来
③调用build()方法返回一个设置好的Notification对象
eg:
一个Builder至少包含:
a small icon 用setSmallIcon来设置
a title 用setContentTitle来设置
detail text 由setContentText来设置
eg:
NotificationCompat.Builder mBuilder = new NotificationCompat.builder(this).setSmallIcon(R.drawable.notification-icon).setContentTitle("My notification").setContentText("Hello World");
2、Define the notification‘s action
一个通知至少由一个action,action通常让用户通过通知跳转到一个应用的活动中,它们可以看作是产生通知或做更多工作的一个event,在通知中,action由包含与Intent中的pendingIntent来定义,而这个Intent通常在你应用的Acitivity中开始。
如何构造一个pendingIntent取决与你的Activity是什么类型的,当你从通知中开启一个活动时,你必须保存用户的导航体验(回到之前的活动界面),在下面代码中,单击通知打开一个新的活动,拓展了通知的功能,这种情况下就不会创建一个人工的回堆栈。
eg:
Intent resultIntent = new Intent(this,resultAction.Class); //由于点击通知代开的是一个新的活动,并不需要创建一个人工返回栈 PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
3、Set the notification's click behavior
为了将之前创建的pendingIntent和手势联系起来,用Notification.Builder中的合适的方法来实现。比如当用户点击一个通知栏的时候开启一个Activity,通过setContentIntent()方法将PendingIntent加入。
eg:
mBuilder.setContentIntent(resultPendingIntent);
4、Issue the Notification
发通知
① 获得NotificationManager的实例
②用notify方法让通知显示出来
③调用build()方法返回一个设置好的Notification对象
eg:
NotificationCompat.Builder mBuilder; //为notification设置id int mNotificationId = 001; //获得NotificationManager服务的实例 NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //建立一个通知 然后发出来 mNotification.notify(mNotificationId,mBuilder.build());
相关文章推荐
- 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译
- 基本控件文档-UISegment属性----iOS-Apple苹果官方文档翻译
- GoogleServices之GooglePlayService官方文档翻译
- django 1.8 官方文档翻译: 1-2-2 编写你的第一个Django应用,第2部分
- GoogleServices 之 Location Location APIS翻译官方文档 3ff8
- 学习ApacheHttpComponents,HttpCore第一部分,基本翻译官方教程
- SiteMesh官方文档翻译(含Freemarker但不包括velocity部分)
- socket官方文档中文翻译(部分)
- DNN皮肤制作官方文档《DotNetNuke Skinning Guide 》翻译(1)—换肤系统需求分析和设计部分
- django 1.8 官方文档翻译: 1-2-1 编写你的第一个Django应用,第1部分
- 基本控件文档-UIKit结构图---iOS-Apple苹果官方文档翻译
- Google Android官方文档进程与线程(Processes and Threads)翻译
- 基本控件文档-UISwitch属性---iOS-Apple苹果官方文档翻译
- 基本控件文档-UIView属性---iOS-Apple苹果官方文档翻译
- 【Tomcat 6.0官方文档翻译】—— 简介
- erlang的官方文档部分翻译
- Apache Cassandra 1.1 项目介绍(逐篇翻译官方文档加一部分评论)
- 谷歌字体(Google Font)初探 [翻译自Google官方文档]
- 谷歌字体(Google Font)初探 [翻译自Google官方文档]
- 【iOS官方文档翻译】iOS蓝牙的基本概念