Android Notification

来源:互联网 发布:追梦设计美工学院 编辑:程序博客网 时间:2024/06/16 05:00

转载:http://blog.csdn.net/liuhaomatou/article/details/21651411

参考文章:http://www.cnblogs.com/newcj/archive/2011/03/14/1983782.html


Notification可以理解为通知的意思一般用来显示广播信息 用Notification就必须要用到NotificationManager

想用Notification一般有三个步骤,如下所示

 一般获得系统级的服务NotificationManager。

              调用Context.getSystemService(NOTIFICATION_SERVICE)方法即可返回NotificationManager实例

 实例化Notification,并设置其属性

              用Notification构造函数 public Notification(int icon, CharSequence tickerText, long when)构造Notification实例 

 通过NotificationManager发通知就OK了

              NotificationManager有两个方法:notify()发出通知  cancel(...)取消通知


下面通过一个代码实例来介绍一下Notification

   先初始化notificationManager和notification两个成员变量

[java] view plaincopyprint?
  1. notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);  
  2. notification = new Notification(R.drawable.touxiang,"信息",System.currentTimeMillis());  
  PS:Notification构造函数里传的参数就是这样显示的第一个参数是 图像,第二个是标题 :信息 ,第三个是系统时间 (见图一) 之所以在这说一下这个构造函数是想与下面的setLatestEventInfo(...)作个区别。

         

                                         (  图一   )                                                                                           (   图二    )

   布局就不贴了 是两个Button  看看发送按钮

[java] view plaincopyprint?
  1. sendButton.setOnClickListener(new OnClickListener()  
  2. {  
  3.       
  4.     @Override  
  5.     public void onClick(View v)  
  6.     {  
  7.         Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class);  
  8.             PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this0, intent, 0);  
  9.             notification.setLatestEventInfo(NotificationActivity.this"你的一条信息""来自张三的信息", pendingIntent);  
  10.             notificationManager.notify(ID,notification);  
  11.             notificationManager.notify(ID+1, notification);  
  12.     }  
  13. })  

      setLatestEventInfo(...)里面所传的参数的效果图:(见图二)通知里面有两条完全一样的消息,是的 你没看错,这是因为我用notificationManager  notify(通知)了两次 而且ID不同,ID是int型是通知信息的标示符。虽然我上面两条信息是一模一样的,但由于ID的不同 , 所以Android还是会显示两条信息。
          在此说一下参数pendingIntent的在setLatestEventInfo里所扮演的角色,是啥子意思呢?pendingIntent可以在另外的地方执行,不是立即意图。当用户点击扩展通知的时候 pendingIntent意图才开始执行,例如图二 我点击其中一个消息后,立马就进入另外一个Activity...


目录

  • 1 状态通知--Status Notifications
  • 2 基础-The Basics
  • 3 响应通知-Responding to Notifications
  • 4 管理通知-Managing your Notifications
  • 5 创建通知-Creating a Notification
  • 6 更新通知-Updating the notification
  • 7 添加声音-Adding a sound
  • 8 添加振动-Adding vibration
  • 9 添加闪灯-Adding flashing lights
  • 10 更多特性-More features
  • 11 自定义通知的布局-Creating a Custom Notification Layout

状态通知--Status Notifications


一个状态通知添加一个图标到系统的状态栏(带有可选的ticker-text消息)和通知消息到通知窗口。当用户选择了一个通知,Android会启动一个Intent,这个Intent是由Notification定义的(通常是为了启动一个Activity)你也可以自定义通知来通知用户,例如用声音文件,振动或者是闪动的通知灯。

当一个后台服务需要通知用户一个需要反应的事件的时候,状态通知就要被用到了。后台服务永远不需要启动一个activity与用户互动,而是应该创建一个状态通知--当用户选择的时候可以启动一个activity。

如图1 在状态栏的左边有一个通知的图标

status_bar.png

图1 状态栏上的图标

如图2 显示在通知窗口里的信息

notifications_window.png

图2 通知窗口

设计-Notification Design

想了解设计准则,请阅读 Android Design's Notifications指南

基础-The Basics

一个Activity或者Service可以初始化一个状态通知。然而,因为一个activity只有运行在前台并且它所在的窗口获得焦点的时候,它才能执行动作,所以你通常需要用service来创建通知。当用户正在使用其他应用或者设备待机时,通知可以由后台创建。为了创建通知,你必须用到两个类:Notification和NotificationManager。

使用Notification类的一个实例去定义状态通知的属性,如图标,通知信息和一些其他的设定,如播放的声音。NotificationManager是系统服务,它来执行和管理所有的状态通知。你不需要直接初始化NotificationManager。为了把你的通知给它,你必须使用getSystemService()检索到指向NotificationManager的引用,然后,当你要通知用户的时候,使用android.app.Notification) notify()把你的Notification传给它。

按如下方式创建状态通知:

1. 得到指向NotificationManager的引用:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. String ns = Context.NOTIFICATION_SERVICE;  
  2. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 2. 初始化Notification:  
  2.    
  3. int icon = R.drawable.notification_icon;  
  4. CharSequence tickerText = "Hello";  
  5. long when = System.currentTimeMillis();  
  6.    
  7. Notification notification = new Notification(icon, tickerText, when);  
  8.    
  9. 3. 定义通知的信息和PendingIntent:  
  10.    
  11. Context context = getApplicationContext();  
  12. CharSequence contentTitle = "My notification";  
  13. CharSequence contentText = "Hello World!";  
  14. Intent notificationIntent = new Intent(this, MyClass.class);  
  15. PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent, 0);  
  16.    
  17. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  
  18.    
  19. 4. 把Notification传递给NotificationManager:  
  20.    
  21. private static final int HELLO_ID = 1;  
  22.    
  23. mNotificationManager.notify(HELLO_ID, notification);  


[java] view plaincopy
  1.    

响应通知-Responding to Notifications

关于通知,用户体验的核心围绕在怎样使它与应用程序的界面相结合。你必须正确地去实现它以便于为你的应用带来统一的用户体验。

日历应用和电子邮件的两个典型的通知例子:活动即将开始的通知;新邮件到来的通知。他们所展现的是两个被推荐的处理通知的形式:或者启动一个与应用无关的独立的activity,或者调用一个应用的全新的实例。

接下来的场景展示了activity栈应该如何在这两种典型的通知流里面工作,首先来操作日历的通知:

1. 用户正在日历应用里创建一个新的活动。他们意识到需要复制一部分电子邮件消息到这个活动中来。

2. 用户选择Home>Email

3. 当操作Email应用时,用户收到从日历应用那里收到一个即将开始的会议的通知。

4. 用户选择了通知,他们开始关注那个日历应用所展现出来的简要的会议信息。

5. 用户已经确定了他们即将有个会议,所以他们按了返回键。他们现在返回了Email应用--接到通知的地方。

操作电子邮件的通知:

1. 目前用户正在电子邮件应用里撰写邮件,需要核对一下日期。

2. 用户选择Home>Calendar(日历应用)

3. 而在日历应用中,他们收到电子邮件关于一个新的讯息的通知。

4. 用户选择了通知,这使他们回到了电子邮件展示详细信息的页面。这取代了他们以前所做的(写信),但这信件仍然保存在草稿箱中。

5. 用户按下了Back键,来到了信件列表页面,然后按下Back键返回日历应用。

在电子邮件那样风格的通知中,按下通知所调出的UI以一种形式展示出通知的应用。例如,当电子邮件应用通过通知来到前台展示的时候,他展示出的是一个列表还是一个具体信件信息要依赖于新邮件的数量,是多封还是一封。为了达到这种效果,我们需要用一个新的acitivity栈所展现出的新通知的状态来完全代替当下的任何一种状态。

下面的代码举例说明了显示这种通知。最关键的是makemessageintentstack()方法,这个方法构建了一个由intent组成的数组-代表了在这种状态下,应用的新的activity栈(如果你正在使用片段(fragments),你可能需要初始化你的片段和应用的状态,从而按下Back键时UI会返回到前一次的状态),其中的核心是Intent.makeRestartActivityTask(),它在栈里构建了根activity,并使用了合适的标志(flag),如Intent.FLAG_ACTIVITY_CLEAR_TASK

[java] view plaincopy
  1.  <pre code_snippet_id="248129" snippet_file_name="blog_20140321_3_3736613" name="code" class="java">/** 
  2.  * This method creates an array of Intent objects representing the 
  3.  * activity stack for the incoming message details state that the 
  4.  * application should be in when launching it from a notification. 
  5.  */  
  6. static Intent[] makeMessageIntentStack(Context context, CharSequence from,  
  7.         CharSequence msg) {  
  8.     // A typical convention for notifications is to launch the user deeply  
  9.     // into an application representing the data in the notification; to  
  10.     // accomplish this, we can build an array of intents to insert the back  
  11.     // stack stack history above the item being displayed.  
  12.     Intent[] intents = new Intent[4];  
  13.    
  14.     // First: root activity of ApiDemos.  
  15.     // This is a convenient way to make the proper Intent to launch and  
  16.     // reset an application's task.  
  17.     intents[0] = Intent.makeRestartActivityTask(new ComponentName(context,  
  18.             com.example.android.apis.ApiDemos.class));  
  19.    
  20.     // "App"  
  21.     intents[1] = new Intent(context, com.example.android.apis.ApiDemos.class);  
  22.     intents[1].putExtra("com.example.android.apis.Path""App");  
  23.     // "App/Notification"  
  24.     intents[2] = new Intent(context, com.example.android.apis.ApiDemos.class);  
  25.     intents[2].putExtra("com.example.android.apis.Path""App/Notification");  
  26.    
  27.     // Now the activity to display to the user.  Also fill in the data it  
  28.     // should display.  
  29.     intents[3] = new Intent(context, IncomingMessageView.class);  
  30.     intents[3].putExtra(IncomingMessageView.KEY_FROM, from);  
  31.     intents[3].putExtra(IncomingMessageView.KEY_MESSAGE, msg);  
  32.    
  33.     return intents;  
  34. }  
  35.    
  36. /** 
  37.  * The notification is the icon and associated expanded entry in the 
  38.  * status bar. 
  39.  */  
  40. void showAppNotification() {  
  41.     // look up the notification manager service  
  42.     NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  43.    
  44.     // The details of our fake message  
  45.     CharSequence from = "Joe";  
  46.     CharSequence message;  
  47.     switch ((new Random().nextInt()) % 3) {  
  48.         case 0: message = "r u hungry?  i am starved"break;  
  49.         case 1: message = "im nearby u"break;  
  50.         default: message = "kthx. meet u for dinner. cul8r"break;  
  51.     }  
  52.    
  53.     // The PendingIntent to launch our activity if the user selects this  
  54.     // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there  
  55.     // is already an active matching pending intent, cancel it and replace  
  56.     // it with the new array of Intents.  
  57.     PendingIntent contentIntent = PendingIntent.getActivities(this0,  
  58.             makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);  
  59.    
  60.     // The ticker text, this uses a formatted string so our message could be localized  
  61.     String tickerText = getString(R.string.imcoming_message_ticker_text, message);  
  62.    
  63.     // construct the Notification object.  
  64.     Notification notif = new Notification(R.drawable.stat_sample, tickerText,  
  65.             System.currentTimeMillis());  
  66.    
  67.     // Set the info for the views that show in the notification panel.  
  68.     notif.setLatestEventInfo(this, from, message, contentIntent);  
  69.    
  70.     // We'll have this notification do the default sound, vibration, and led.  
  71.     // Note that if you want any of these behaviors, you should always have  
  72.     // a preference for the user to turn them off.  
  73.     notif.defaults = Notification.DEFAULT_ALL;  
  74.    
  75.     // Note that we use R.layout.incoming_message_panel as the ID for  
  76.     // the notification.  It could be any integer you want, but we use  
  77.     // the convention of using a resource id for a string related to  
  78.     // the notification.  It will always be a unique number within your  
  79.     // application.  
  80.     nm.notify(R.string.imcoming_message_ticker_text, notif);  
  81. }  
  82.    
  83. 在日历应用风格的通知中,被通知调用的UI是一个专用的activity,而不是应用流程中的一部分。例如,当用户接受到日历应用的通知时,点击查看,触发了一个特殊的activity来显示即将开始的活动的列表-这个界面只能由通知调用,而不能由日历应用中的界面进入。  
  84. 这种通知的代码是很简单的,就像上面的代码,但是PendingIntent只能用于单个的activity,我们那个专门的通知activity。  
  85.    
  86. /** 
  87.  * The notification is the icon and associated expanded entry in the 
  88.  * status bar. 
  89.  */  
  90. void showInterstitialNotification() {  
  91.     // look up the notification manager service  
  92.     NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  93.    
  94.     // The details of our fake message  
  95.     CharSequence from = "Dianne";  
  96.     CharSequence message;  
  97.     switch ((new Random().nextInt()) % 3) {  
  98.         case 0: message = "i am ready for some dinner"break;  
  99.         case 1: message = "how about thai down the block?"break;  
  100.         default: message = "meet u soon. dont b late!"break;  
  101.     }  
  102.    
  103.     // The PendingIntent to launch our activity if the user selects this  
  104.     // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there  
  105.     // is already an active matching pending intent, cancel it and replace  
  106.     // it with the new Intent.  
  107.     Intent intent = new Intent(this, IncomingMessageInterstitial.class);  
  108.     intent.putExtra(IncomingMessageView.KEY_FROM, from);  
  109.     intent.putExtra(IncomingMessageView.KEY_MESSAGE, message);  
  110.     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);  
  111.     PendingIntent contentIntent = PendingIntent.getActivity(this0,  
  112.             intent, PendingIntent.FLAG_CANCEL_CURRENT);  
  113.    
  114.     // The ticker text, this uses a formatted string so our message could be localized  
  115.     String tickerText = getString(R.string.imcoming_message_ticker_text, message);  
  116.    
  117.     // construct the Notification object.  
  118.     Notification notif = new Notification(R.drawable.stat_sample, tickerText,  
  119.             System.currentTimeMillis());  
  120.    
  121.     // Set the info for the views that show in the notification panel.  
  122.     notif.setLatestEventInfo(this, from, message, contentIntent);  
  123.    
  124.     // We'll have this notification do the default sound, vibration, and led.  
  125.     // Note that if you want any of these behaviors, you should always have  
  126.     // a preference for the user to turn them off.  
  127.     notif.defaults = Notification.DEFAULT_ALL;  
  128.    
  129.     // Note that we use R.layout.incoming_message_panel as the ID for  
  130.     // the notification.  It could be any integer you want, but we use  
  131.     // the convention of using a resource id for a string related to  
  132.     // the notification.  It will always be a unique number within your  
  133.     // application.  
  134.     nm.notify(R.string.imcoming_message_ticker_text, notif);  
  135. }  
  136.    
  137. 然而这还不够。通常Android认为一个应用里的所有activity都是这个应用界面流程的一部分,所以像这样简单地调用activity能够导致:使这个activity被混入应用的返回栈中。为了让它正确的工作,在manifest声明的时候,android:launchMode="singleTask", android:taskAffinity="" 和android:excludeFromRecents="true"是必须要设置的。例子如下:  
  138.    
  139. <activity android:name=".app.IncomingMessageInterstitial"  
  140.         android:label="You have messages"  
  141.         android:theme="@style/ThemeHoloDialog"  
  142.         android:launchMode="singleTask"  
  143.         android:taskAffinity=""  
  144.         android:excludeFromRecents="true">  
  145. </activity></pre><br>  

你必须要小心翼翼地通过这个初始的activity来调用其他的activity,因为它不是应用的最上层,也没有在最近出现过,而且它需要被通知调用,任何时候。最好的方法就是确保任何被它调用的activity都是在它自己的任务中被调用的。当这么做的时候,必须要小心,确保这个新的任务和当前存在于应用中的任务能够很好的交互。这在本质上与之前提到的电子邮件风格的通知,切换到主界面的情况一样。想想之前的makeMessageIntentStack() ,处理一个点击事件然后看起来是这样的:

[java] view plaincopy
  1. <span style="color:#666666"> </span><pre code_snippet_id="248129" snippet_file_name="blog_20140321_4_9657411" name="code" class="java">/** 
  2.  * Perform a switch to the app.  A new activity stack is started, replacing 
  3.  * whatever is currently running, and this activity is finished. 
  4.  */  
  5. void switchToApp() {  
  6.     // We will launch the app showing what the user picked.  In this simple  
  7.     // example, it is just what the notification gave us.  
  8.     CharSequence from = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_FROM);  
  9.     CharSequence msg = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_MESSAGE);  
  10.     // Build the new activity stack, launch it, and finish this UI.  
  11.     Intent[] stack = IncomingMessage.makeMessageIntentStack(this, from, msg);  
  12.     startActivities(stack);  
  13.     finish();  
  14. }</pre><br><span style="color:#808080"><em><br></em></span>  

管理通知-Managing your Notifications

NotificationManager是系统服务,负责管理所有的通知。你必须使用getSystemService()得到它的引用。如下:

[java] view plaincopy
  1.  <pre code_snippet_id="248129" snippet_file_name="blog_20140321_5_3277699" name="code" class="java">String ns = Context.NOTIFICATION_SERVICE;  
  2. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);</pre><br>  

当你要发表通知时,要使用notify(int, Notification)把Notification传递给NotificationManager。方法的第一个参数是通知的唯一的ID,第二个参数就是Notification对象。ID唯一地标识了通知是来自你的应用程序。如果你需要更新通知或者(如果你的应用管理不同种类的通知)当用户通过通知中定义的intent返回到你的应用时,选择合适的action,那么ID就是必需的。

当用户点击选择通知后,为了清空通知,给你的Notification添加一个标志""FLAG_AUTO_CANCEL",你也可以使用cancel(int)手动清空它,参数是通知的ID,或者使用cancelAll()清空所有通知。

创建通知-Creating a Notification

Notification对象定义了通知的细节信息和其他提醒的设置,例如声音和闪灯。

一个状态通知需要以下:

  • 状态栏的图标
  • 标题和信息,除非你定义一个custom notification layout
  • PendingIntent,当通知被点击选择时将会被触发。

可选的设定包括:

  • 标题栏的滚动文本
  • 提醒的声音
  • 振动的设定
  • 闪屏的设定

新通知的starter-kit包括Notification(int, CharSequence, long)构造函数和setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)方法。这些为通知定义了所有的需求。下面是一个基本的通知:

[java] view plaincopy
  1.  <pre code_snippet_id="248129" snippet_file_name="blog_20140321_6_7421374" name="code" class="java">int icon = R.drawable.notification_icon;        // icon from resources  
  2. CharSequence tickerText = "Hello";              // ticker-text  
  3. long when = System.currentTimeMillis();         // notification time  
  4. Context context = getApplicationContext();      // application Context  
  5. CharSequence contentTitle = "My notification";  // message title  
  6. CharSequence contentText = "Hello World!";      // message text  
  7.    
  8. Intent notificationIntent = new Intent(this, MyClass.class);  
  9. PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent, 0);  
  10.    
  11. // the next two lines initialize the Notification, using the configurations above  
  12. Notification notification = new Notification(icon, tickerText, when);  
  13. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent</pre>   

更新通知-Updating the notification

当事件继续出现在你的应用中的时候,你就可以更新状态通知的信息。例如,当上一个条短信被阅读之前又来了一条新短信,这时短信应用会更新已存在的通知,给未读信息条数加一。这个更新通知的实践要比添加一个新通知好得多,因为它避免了在通知窗口里遇到的混乱。

因为每个通知都是由NotificationManager定义的,有着唯一的ID,你可以通过调用setLatestEventInfo()来修改通知,然后再调用一次notify().

你可以修改对象的每个属性(除了Context和通知的标题和内容)。你应该随时修改文本信息当调用setLatestEventInfo()并且contentTitle和contentText都有新值的时候,然后调用notify()(当然,如果你已经创建了custom notification layout,那么更新标贴和文本是不起作用的)

添加声音-Adding a sound

你可以使用默认的声音文件或者自定义的声音文件来提醒用户。

为了使用默认声音,要给defaults属性赋值"DEFAULT_SOUND":

[java] view plaincopy
  1.  <pre code_snippet_id="248129" snippet_file_name="blog_20140321_7_7661354" name="code" class="java">notification.defaults |= Notification.DEFAULT_SOUND;</pre><br>  

为了给你的通知使用不同的声音,需要把声音文件的Uri传递给sound属性。如下:

[java] view plaincopy
  1. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_8_3756877" name="code" class="java">notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");</pre>  

在下面的例子里,我们从内部的MediaStore's ContentProvider:

[java] view plaincopy
  1.    
  2. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_9_3996857" name="code" class="java">notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");</pre>   

在上面的例子中,数字6是媒体文件的ID,而且被加在了Uri的后面。如果你不知道确切的ID,你必须使用ContentResolver在MediaStore要查询一下。你可以查看Content Providers文档,来了解如何使用ContentResolver。

如果你希望提示音可以反复的播放,直到用户对通知做出了反应或者通知被取消了,可以把FLAG_INSISTENT赋值给flags属性。

注意:如果defaults属性的值是DEFAULT_SOUND,那么无论设置什么声音都不会有效果的,仍只会播放默认的声音。

添加振动-Adding vibration

你可以用默认的振动方式或自定义的振动方式来提示用户。

默认的方式,要用到DEFAULT_VIBRATE

[java] view plaincopy
  1. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_10_4236837" name="code" class="java">notification.defaults |= Notification.DEFAULT_VIBRATE;</pre>  

自定义的方式,要是定义一个long型数组,赋值给vibrate属性:

[java] view plaincopy
  1. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_11_1237431" name="code" class="java">long[] vibrate = {0,100,200,300};  
  2. notification.vibrate = vibrate;</pre>   

long型的数组定义了交替振动的方式和振动的时间(毫秒)。第一个值是指振动前的准备(间歇)时间,第二个值是第一次振动的时间,第三个值又是间歇的时间,以此类推。振动的方式任你设定。但是不能够反复不停。

注意:如果defaults属性的值是DEFAULT_VIBRATE,那么无论设置什么振动都不会有效果的,仍只会以默认的方式振动。

添加闪灯-Adding flashing lights

使用闪灯来提示用户,你可以使用默认的也可以自定义

默认的方式,DEFAULT_LIGHTS

[java] view plaincopy
  1. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_12_7332953" name="code" class="java">notification.defaults |= Notification.DEFAULT_LIGHTS;</pre>   

可以自定义灯光的颜色和闪动的方式。ledARGB属性是定义颜色的,ledOffMS属性是定义灯光关闭的时间(毫秒),ledOnMs是灯光打开的时间(毫秒),还要给flags属性赋值为FLAG_SHOW_LIGHTS

[java] view plaincopy
  1.    
  2. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_13_4541291" name="code" class="java">notification.ledARGB = 0xff00ff00;  
  3. notification.ledOnMS = 300;  
  4. notification.ledOffMS = 1000;  
  5. notification.flags |= Notification.FLAG_SHOW_LIGHTS;</pre><br>   

上面的例子中,绿色的灯亮了300毫秒,暗了1秒,,,,如此循环。

更多特性-More features

你可以使用Notification和标志(flags)来为自己的通知做定制。下面是一些有用的特性:

FLAG_AUTO_CANCEL标志

使用这个标志可以让在通知被选择之后,通知提示会自动的消失。

FLAG_INSISTENT标志

使用这个标志,可以让提示音循环播放,知道用户响应。

FLAG_ONGOING_EVENT标志

使用这个标记,可以让该通知成为正在运行的应用的通知。 这说明应用还在运行-它的进程还跑在后台,即使是当应用在前台不可见(就像音乐播放和电话通话)。

FLAG_NO_CLEAR标志

使用这个标志,说明通知必须被清除,通过"Clear notifications"按钮。如果你的应用还在运行,那么这个就非常有用了。

number属性

这个属性的值,指出了当前通知的数量。这个数字是显示在状态通知的图标上的。如果你想使用这个属性,那么当第一个通知被创建的时候,它的值要从1开始。而不是零。

iconLevel

这个属性的值,指出了LevelListDrawable的当前水平。你可以通过改变它的值与LevelListDrawable定义的drawable相关联,从而实现通知图标在状态栏上的动画。查看LevelListDrawable可以获得更多信息。

查看Notification可以了解更多。

自定义通知的布局-Creating a Custom Notification Layout

custom_message.png

图3 自定义布局

默认的,通知窗口里的通知会包括标题和消息文本两部分。它们是通过setLatestEventInfo()定义了contentTitle和contentText来实现的。然而,你也可以使用RemoteViews为通知界面定义一个布局。它看起来与默认的布局很像,但实际上是由XML创建的。

为了自定义通知的布局,首先实例化RemoteViews创建一个布局文件,然后把RemoteViews传递给Notification的contentView属性。

通过下面的例子可以更好的理解:

1. 创建一个XML布局文件 custom_notification.xml:

[xml] view plaincopy
  1.    
  2. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_14_636814" name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:padding="10dp" >  
  7.     <ImageView android:id="@+id/image"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="fill_parent"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_marginRight="10dp" />  
  12.     <TextView android:id="@+id/title"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_toRightOf="@id/image"  
  16.         style="@style/NotificationTitle" />  
  17.     <TextView android:id="@+id/text"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_toRightOf="@id/image"  
  21.         android:layout_below="@id/title"  
  22.         style="@style/NotificationText" />  
  23. </RelativeLayout>  
  24.    
  25. 注意那两个TextView的style属性。在定制的通知界面中,为文本使用style文件进行定义是很重要的,因为通知界面的背景色会因为不同的硬件,不同的os版本而改变。从android2.3(API 9)开始,系统为默认的通知界面定义了文本的style属性。因此,你应该使用style属性,以便于在android2.3或更高的版本上可以清晰地显示你的文本,而不被背景色干扰。  
  26. 例如,在低于android2.3的版本中使用标准文本颜色,应该使用如下的文件res/values/styles.xml:  
  27.    
  28. <?xml version="1.0" encoding="utf-8"?>  
  29. <resources>  
  30.     <style name="NotificationText">  
  31.       <item name="android:textColor">?android:attr/textColorPrimary</item>  
  32.     </style>  
  33.     <style name="NotificationTitle">  
  34.       <item name="android:textColor">?android:attr/textColorPrimary</item>  
  35.       <item name="android:textStyle">bold</item>  
  36.     </style>  
  37.     <!-- If you want a slightly different color for some text,  
  38.          consider using ?android:attr/textColorSecondary -->  
  39. </resources>  
  40.  </pre><br><br>  

然后,在高于android2.3的系统中使用系统默认的颜色,如下文件res/values-v9/styles.xml:

[xml] view plaincopy
  1. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_15_320386" name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
  4.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
  5. </resources></pre><br>   

现在,当运行在2.3版本以上时,在你的定制界面中,文本都会是同一种颜色-系统为默认通知界面定义的颜色。这很重要,能保证你的文本颜色是高亮的,即使背景色是意料之外的颜色,你的文本页也会作出适当的改变。

2. 现在,在应用的代码中,使用RemoveViews方法定义了图片和文本。然后把RemoveViews对象传给contentView属性。例子如下:

[java] view plaincopy
  1.  <pre code_snippet_id="248129" snippet_file_name="blog_20140321_16_7528723" name="code" class="java">RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);  
  2. contentView.setImageViewResource(R.id.image, R.drawable.notification_image);  
  3. contentView.setTextViewText(R.id.title, "Custom notification");  
  4. contentView.setTextViewText(R.id.text, "This is a custom layout");  
  5. notification.contentView = contentView;</pre><br><br><br>  

如上所示,把应用的包名和布局文件的ID传给RemoteViews的构造器。然后分别使用setImageViewResource()和setTextViewText()定义ImageView和TextView的内容。最后,吧RemoteViews对象传递给通知的contentView属性。

3.因为当你在使用定制界面时,不需要使用setLatestEventInfo(),你必须为通知定义一个intent,并赋值给contentIntent属性,如下代码:

[java] view plaincopy
  1.    
  2. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_17_5260448" name="code" class="java">Intent notificationIntent = new Intent(this, MyClass.class);  
  3. PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent, 0);  
  4. notification.contentIntent = contentIntent;</pre><br><br>  

4.发送通知:

[java] view plaincopy
  1.    
  2. <pre code_snippet_id="248129" snippet_file_name="blog_20140321_18_309196" name="code" class="java">mNotificationManager.notify(CUSTOM_VIEW_ID, notification);</pre>   

RemoteViews类还包括一些方法可以让你轻松地在通知界面的布局里添加Chronometer和ProgressBar。如果要为你的通知界面做更多的定制,请参考RemoteViews。

特别注意:当创建一个自定义通知布局,你必须特别小心,以确保布局能地在不同的设备上适当地显示。而这个建议适用于所有视图布局创建,不单单是通知界面布局,在这种情况下(通知界面)尤其重要,因为是布局的空间是非常有限的。所以不要让你的自定义布局太复杂并确报在不同的配置上测试它。

0 0