Android 中的消息通知Notification


用惯了Android的人在刚拿到iPhone的时候,总是会习惯性的用手指从状态栏往下拖一下,这都是给Notification闹的。
不过Notification也确实是1个不错的提示工具,不干扰正常的操作,事后还可以再翻看详细的内容,点击后还可以进入相关的画面查看更具体的内容。
今天我就以代码为主的形式来介绍Notification的使用,包括基本用法,自定义的View,以及更多的控制方法。
另一种Android中常用到的提示方法Toast的用法请参见《教程:在Android中使用Toast进行提示》
我们先看下Notification的几个主要组成部分:
Icon:不解释
Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动
Icon和Ticker Text
Content Title:Notification展开后的标题
Content Text:Notification展开后的内容
Content Title和Text

 

Notification的一般用法

取得NotificationManager

Java代码   收藏代码
  1. private NotificationManager mNotificationManager;  
  2. mNotificationManager = (NotificationManager)   
  3.     getSystemService(Context.NOTIFICATION_SERVICE);  
  

创建Notification并且显示

Java代码   收藏代码
  1. //Notification的滚动提示  
  2. String tickerText = "My notification, It's a long text! Hello World desiyo?";  
  3. //Notification的图标,一般不要用彩色的  
  4. int icon = R.drawable.icon_02241_3;  
  5.    
  6. //contentTitle和contentText都是标准的Notification View的内容  
  7. //Notification的内容标题,拖下来后看到的标题  
  8. String contentTitle="My notification";  
  9. //Notification的内容  
  10. String contentText="Hello World!";  
  11.    
  12. //Notification的Intent,即点击后转向的Activity  
  13. Intent notificationIntent = new Intent(thisthis.getClass());  
  14. notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  15. PendingIntent contentIntent = PendingIntent.getActivity(this0,   
  16.     notificationIntent, 0);  
  17.    
  18. //创建Notifcation  
  19. Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());  
  20. //设定Notification出现时的声音,一般不建议自定义  
  21. notification.defaults |= Notification.DEFAULT_SOUND;  
  22. //设定如何振动  
  23. notification.defaults |= Notification.DEFAULT_VIBRATE;  
  24. //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身  
  25. //这符合一般的Notification的运作规范  
  26. notification.flags|=Notification.FLAG_AUTO_CANCEL;  
  27. notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);  
  28. //显示这个notification  
  29. mNotificationManager.notify(HELLO_ID, notification);  
  

这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。

使用自定义View的Notification

同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在Android Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。
首先给出View的定义文件:notification_view_sample.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:padding="3dp"  
  7. >  
  8.     <ImageView android:id="@+id/notificationImage"  
  9.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  10.         android:src="@android:drawable/stat_sys_download"  
  11.     />  
  12.     <TextView android:id="@+id/notificationTitle"  
  13.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  14.         android:layout_toRightOf="@id/notificationImage"  
  15.         android:layout_alignParentRight="true"  
  16.         android:paddingLeft="6dp"  
  17.         android:textColor="#FF000000"  
  18.     />  
  19.     <TextView android:id="@+id/notificationPercent"  
  20.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  21.         android:layout_below="@id/notificationImage"  
  22.         android:paddingTop="2dp"  
  23.         android:textColor="#FF000000"  
  24.     />  
  25.     <ProgressBar android:id="@+id/notificationProgress"  
  26.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  27.         android:layout_below="@id/notificationTitle"  
  28.         android:layout_alignLeft="@id/notificationTitle"  
  29.         android:layout_alignParentRight="true"  
  30.         android:layout_alignTop="@id/notificationPercent"  
  31.         android:paddingLeft="6dp"  
  32.         android:paddingRight="3dp"  
  33.         android:paddingTop="2dp"  
  34.         style="?android:attr/progressBarStyleHorizontal"  
  35.     />  
  36. </RelativeLayout>  
 
 

接下来是Java代码片段:

Java代码   收藏代码
  1. //Notification的滚动提示  
  2. String tickerText1 = "Custom view for download notification";  
  3. //Notification的图标,一般不要用彩色的  
  4. int icon1 = android.R.drawable.stat_sys_download;  
  5.    
  6. //Notification的Intent,即点击后转向的Activity  
  7. Intent notificationIntent1 = new Intent(thisthis.getClass());  
  8. notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  9. PendingIntent contentIntent1 = PendingIntent.getActivity(this0, notificationIntent1, 0);  
  10.    
  11. //创建Notifcation  
  12. Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());  
  13. //设定Notification出现时的声音,一般不建议自定义  
  14. notification1.defaults |= Notification.DEFAULT_SOUND;  
  15. //设定是否振动  
  16. notification1.defaults |= Notification.DEFAULT_VIBRATE;  
  17. //notification.number=numbers++;  
  18. //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身  
  19. //这符合一般的Notification的运作规范  
  20. notification1.flags|=Notification.FLAG_ONGOING_EVENT;  
  21.    
  22. //创建RemoteViews用在Notification中  
  23. RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample);  
  24. contentView.setTextViewText(R.id.notificationTitle, "Download:Facebook for android");  
  25. contentView.setTextViewText(R.id.notificationPercent, "35%");  
  26. contentView.setProgressBar(R.id.notificationProgress, 10035false);  
  27.    
  28. notification1.contentView = contentView;  
  29. notification1.contentIntent=contentIntent1;  
  30.    
  31. //显示这个notification  
  32. mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);  
 

注意以上代码中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,这都说明了Notification是1个“远程”的东西,其中能够使用的控件是受限制的,比如说TableLayout就不能使用。看下效果图,是不是和 Market中的界面很接近呢?
自定义View的Notification效果图

更好的控制Notification

动画图标怎么做?

和selector类似,定义1个XML文件放在drawable下,下面是之前用到的stat_sys_download的定义:

Xml代码   收藏代码
  1. <animation-list  
  2.         xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:oneshot="false">  
  4.     <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" />  
  5.     <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" />  
  6.     <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" />  
  7.     <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" />  
  8.     <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" />  
  9.     <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" />  
  10. </animation-list>  
如何更新Notification?

注意到前面的代码中用到的CUSTOM_VIEW_ID,这是Notification的ID,如果2次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒。之前给出的ProgressBar是不会动的,利用这个方法就可以让它动起来(或者也可以直接调用RemoteView的set方法来直接更新?未试验)

如何自定义提示的声音和振动?
//自定义提示音
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
//自定义振动方式
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;

请注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,则自定义的提示音和振动无效。

在类似于短消息的应用中如何提示数量?

使用Notification的number属性,默认为0,如果是1或更大的数字,则会在图标上覆盖显示这个数字。
notification.number=notificationNumber;

Flag的使用

notification有1个flag属性,除了DEFAULT_SOUND之外,还有几个很有用的属性。
FLAG_AUTO_CANCEL:自动清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用户响应(很吵吧!)
FLAG_ONGOING_EVENT:表示这是1个正在进行的任务,不可以清除,第2个例子中有用到
FLAG_NO_CLEAR:不可以清除



转自:http://sdfiyon.iteye.com/blog/1386392







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android常驻通知(Notification)是指在用户状态栏一直显示的通知图标和文本内容,不会因为用户操作或应用进程被销毁而消失。常驻通知通常用于实时监测、后台服务、音乐播放等需要持续提醒用户的场景。 常驻通知的实现步骤如下: 1. 首先,需要创建一个Notification对象,包括通知图标、标题、内容等信息。 2. 然后,创建一个PendingIntent,用于定义用户点击通知后的操作,比如打开应用的某个Activity或执行某个Service。 3. 创建一个NotificationChannel(通知渠道),用于定义通知的重要程度,包括声音、震动等设置。 4. 将Notification对象与PendingIntent关联,并将其设置为常驻通知的优先级。 5. 最后,调用NotificationManager的notify方法,将通知显示在用户的状态栏上。 需要注意的是,常驻通知存在一些使用限制和最佳实践: 1. 用户可以通过设置通知管理来关闭或打开特定应用的常驻通知。 2. 常驻通知不适合用于广告或频繁推送的内容,以免打扰用户。 3. 为了避免误导用户,常驻通知的图标和文本内容应与应用的实际情况相符。 4. 如果需要更新通知的内容或操作,可以使用NotificationManager的notify方法进行更新,并保持通知的id不变。 总之,常驻通知Android提供的一个重要功能,可以实现持续提醒用户和后台监测的需求。但应用开发者需要注意使用场景和用户体验,遵循Android的最佳实践,以确保用户对常驻通知的接受和理解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值