我们经常可以在Android手机中下拉状态栏时可以看到有许多的推送通知,Android中专门提供了Notification来实现这种效果,如下:





Notification的基本使用:

创建一个NotificationManager类实例,用来发送通知

NotificationManager manager = (Notification)getSystemService(NOTIFICATION_SERVICE);




创建一个Notification类实例,用来设置通知的各个细节,比如图标、标题、内容..等

Notification notification = new Notification();



通知的设置主要有两个模块,一个是在状态栏上的显示,一个是在下拉状态栏后的显示,如下:





设置通知在状态栏上显示的细节

notification.icon = R.drawable.ic_launcher;//设置通知在状态栏上显示的图标notification.tickerText = "你有一条新消息";//当通知触发时会显示在状态栏上



设置通知在下拉状态栏后显示的细节

//设置下拉状态栏后该通知的布局,可以在自定义Notification布局时使用RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify);notification.contentView = remoteViews;notification.when = System.currentTimeMillis();  //设置通知显示的时间获得一个PendingIntent对象,用来设置用户点击通知时发生的跳转Intent newintent = new Intent(this,MainActiviy.class);PendingIntent pintent = PendingIntent.getActivity(getContext(), 0, newintent, 0);



分析:
可以看到这里有一个PendingIntent对象,PendingIntent对象其实就相当于Intent对象的包装,Intent是立即跳转到某个Activity或者Service等,而PendingIntent是在某个时刻触发了才会进行跳转

要得到一个pendingIntent对象,使用PendingIntent类的静态方法,主要有以下几种:
getActivity(Context, int, Intent, int)   【跳转到一个Activity】
getBroadcast(Context, int, Intent, int) 【打开一个BroadcastReceiver】
getService(Context, int, Intent, int)    【启动一个Service】

可以看到,这里有4个参数,它们分别是:
第一个参数 :当前的上下文对象,比如在Activity中可以通过getContext()获得
第二个参数 :当有多条通知时会用到,如果两个通知的requestCode值一样时,后面的通知就会对之前的通知起作用,配合第四个参数设置
第三个参数 :传入一个Intent对象,这个intent用来做用户点击通知时的跳转
第四个参数 :flag标志位,用来表示intent中的数据的覆盖方式,比如你推送了消息1,并在其中的Intent中putExtra了一个值“ABC”,在未点击该消息前,继续推送第二条消息,并在其中的Intent中putExtra了一个值“CBA”,这时候,如果你设置的是PendingIntent.FLAG_UPDATE_CURRENT,那么当你单击消息1或者消息2,你会发现,他俩个的Intent中读取过来的信息都是“CBA”,就是说,第二个替换了第一个的内容,如果你设置的是PendingIntent.FLAG_CANCEL_CURRENT,会发现点击消息1时,没反应,第二条可以点击。flag的作用只有在第二个参数requestCode一样时才会起作用。



调用setLatestEventInfo

设置完以上细节后还要调用setLatestEventInfo方法来配置Notification在下拉后的通知窗口中的外观,传入当前context对象,以及设置通知的标题和内容,并且将创建好的PendingIntent设置进去
notification.setLastestEventInfo(getContext(), "这里是通知的标题", "这里是通知的内容", pendingIntent);



发送通知

以上都设置完成之后,只需要通过NotificationManager对象来发送通知即可:
manager.notify(1,notification);



分析:这里有两个参数,第一个表示这个通知的ID,用来标识这个通知,第二个参数即设置你要发送的Notification对象
为什么要为Notification设置ID?

如果Notification使用的是相同的ID,那无论发送多少次,通知栏只会显示一条通知
如果是以下这种情况:
manager.notify(1,notification);manager.notify(2,notification);manager.notify(3,notification);


可以看到通知栏出现了三条通知 另外,取消通知的时候也会用到这个ID来标识取消的是哪一个Notification


如何取消通知?

通过通知管理者调用cancel方法,传入一个ID,这个ID就是之前notify时为notification设置的那个ID,传入哪个ID即取消哪个通知:
manager.cancel(1);

另外,也可以取消所有通知:
manager.cancelAll();


通知的个性化设置

Notification还为我们提供了许多提醒的效果,比如振动、铃声、LED灯的闪烁等等
设置系统默认提示音:
notification.defaults = notification.DEFAULT_SOUND;



设置自定义提示音:
Uri soundUri = Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.mysound);notification.sound = soundUri;
Uri路径前面的那段是通用的,如果要修改成其他的声音的话,只需修改文件名(R.raw.mysound)


设置系统默认震动:
notification.defaults = notification.DEFAULT_VIBRATE;



设置自定义震动:
long[] vibrateArgs = new long[]{0,1000,1000,1000,1000,1000};notification.vibrate = vibrateArgs;
这里传入一个long数组,里面的元素表示:0s后开始震动,且持续震动1s,然后停止1s,接着再震动1s,以此类推



设置系统默认三色灯:
notification.defaults = notification.DEFAULT_LIGHTS;



设置自定义三色灯:
notification.ledARGB = 0xff0000ff; //【灯光颜色】notification.ledOnMS = 300;   //【亮持续时间】notification.ledOffMS = 300;   //【暗的时间】



最后附上完整代码:
NotificationManager manager = (NotificationManager)context.getSystemService(Service.NOTIFICATION_SERVICE);Notification notification = new Notification();//设置状态栏的显示notification.tickerText = "您有一条新消息";notification.icon = R.drawable.ic_launcher;//自定义布局RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_item);remoteView.setImageViewResource(R.id.icon, R.drawable.ic_launcher);notification.contentView = remoteView;//设置状态栏下拉后的显示布局以及点击通知后的跳转Intent Noticeintent = new Intent(context,NotificationActivity.class);PendingIntent pintent = PendingIntent.getActivity(context, 0, Noticeintent, 0);notification.setLatestEventInfo(context, "标题", "内容", pintent);//设置震动long[] l = new long[]{0,1000,1000,1000,1000,1000};notification.vibrate = l;//设置声音Uri soundUri = Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.mysound);notification.sound = soundUri;//发送通知manager.notify(1, notification);



更多相关文章

  1. android 设置bitmap 设置图片的大小
  2. android - DefaultHttpClient设置超时.
  3. android常用布局设置
  4. Android开发(十一)——ImageView的尺寸设置scaleType
  5. Android(安卓)网络操作常用的两个类
  6. android:textAppearance
  7. Android(安卓)Wifi获取组播
  8. Android(安卓)ListView控件基本用法
  9. Android开发EditText属性

随机推荐

  1. 修改android公共控件和后台服务
  2. Android布局控件属性
  3. Android(安卓)动态加载(二) - 基础篇(二)
  4. Android布局文件中常用的属性
  5. Android(安卓)SDK离线快速安装教程 Andro
  6. Android(安卓)Studio系列(三)Version Contr
  7. Android(安卓)Layout属性笔记
  8. 详解 Android(安卓)的 Activity 组件
  9. Android技能树 — 排序算法基础小结
  10. Android安全机制探讨