在Android中,基本的Notification就是有事件发生的时候在屏幕顶端的Notification bar上显示一个图标。然后拉下Notification bar,点击Notification的项目,会调用相应的程序做处理。比如有新短信,就会出现短信的图标,拉下Notification bar,点击图标会调用短信查看程序。

我们先看一下Notification的Sample Code,然后逐行做解说,大致能了解它的基本构成。

import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;...private void showNotification(Message msg, int id) {NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);Notification notification = new Notification(R.drawable.notiicon, msg.getTitle(), System.currentTimeMillis());notification.flags = Notification.FLAG_AUTO_CANCEL;Intent intent = new Intent(this, MainActivity.class);Bundle bundle = new Bundle();bundle.putString("info", msg.getInfo());intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent contentIntent = PendingIntent.getActivity(this, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),contentIntent);notiManager.notify(id, notification);}

首先导入三个类,Notification,NotificationManager,PendingIntent。 值得一提的是PendingIntent,它可以看做是Intent这封信的一个信封。PendingIntent基本上是Intent的包装和描述,对象收到PendingIntent后,可以得到其中的Intent再发出去。

NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

上面这一句,从系统中获得Notification服务,getSystemService()就是这么用,来获得系统服务的。

Notification notification = new Notification(R.drawable.notiicon, msg.getTitle(), System.currentTimeMillis());notification.flags = Notification.FLAG_AUTO_CANCEL;

然后是构造一个Notification,包括三个属性,图标,图标后面的文字,以及Notification时间部分显示出来的时间,通常使用当前时间。FLAG_AUTO_CANCEL说明Notification点击一次就消失。

Intent intent = new Intent(this, MainActivity.class);Bundle bundle = new Bundle();bundle.putString("info", msg.getInfo());intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);

上面这部分Code,构造一个Intent,并且放进去一条信息。 FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK者两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。

PendingIntent contentIntent = PendingIntent.getActivity(this, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);

这一句代码把Intent包装在PendingIntent里,this是Context,id是PendingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的。

notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),contentIntent);notiManager.notify(id, notification);

最后这两行添加状态栏的详细信息,包装PendingIntent给Notification,最后发送Notification。

更多相关文章

  1. 【Android开发学习03】短时间的音效播放实现SoundPool
  2. Android(安卓)ui utils-简单实用的Android界面工具
  3. Android启动出现短暂黑屏或白屏解决方案
  4. Android(安卓)获取mp4文件中任意时间点的帧图片(MediaMetadataRe
  5. Android开发问题记录(二)——Eclipse不显示Android(安卓)SDK&AVD M
  6. android系统设置-修改睡眠时间,修改背光强度
  7. Android(安卓)自定义时间选择对话框
  8. Android(安卓)video streaming and encoder
  9. Android(安卓)登录页面(密码显示隐藏、EditText 图标切换、限制输

随机推荐

  1. Android 读取手机音乐,视频,图片
  2. android 消息提醒(Toast,Notification)
  3. Xamarin.Android中使用android:onClick="
  4. Android中ActivityManager学习笔记(2)-Ru
  5. android背景圆角
  6. 超级清爽android计算器界面源码
  7. Android飞行模式下Wifi开关行为的控制
  8. Android获取手机短信
  9. [Android]单元测试实例
  10. CheckedTextView的显示问题