【转】Android通知的使用及简单二次封装

转自:http://www.juwends.com/tech/android/android-notification.html

Android通知就是让设备在屏幕最顶上那栏里面显示图标,当滑下通知栏之后可以看到列表状的通知选项,有些是“通知”类型的,有些是“正在运行”类型的,“通知”类型的通知是可以清除的,“正在运行”类型的通知是无法清除的,比如短信来了,顶上的状态栏就会出现通知,这么通知通常是可以被清除掉的,还比如听音乐的时候出现的通知,这么通知通常就不能清除的、正在运行的类型,具体如何定义这两种类型将会在后面的代码中给出。

下面是产生通知的(弱弱的封装了一下,一般都是直接使用了,不再去直接再写实现,代码里面把显示通知的过程给出了)代码:

/*
 * Copyright (C) 2013 Juwend's Demo
 *
 * 本代码可以任意复制与改动,欢迎转载,转载请注明出处
 * 
 *   http://www.juwends.com/
 * 
 */

package com.juwends.helper;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

/**
 * 通知制造
 * 
 * @author Juwend
 * 
 */
public class NotificationHelper {
 // 1.实例化Notification类
 // 2.设置Notification对象的icon,通知文字,声音
 // 3.实例化PendingIntent类,作为控制点击通知后显示内容的对象
 // 4.加载PendingIntent对象到Notification对象(设置 打开通知抽屉后的 标题/内容)
 // 5.获得 NotificationManager对象
 // 6.使用NotificationManager对象显示通知
 
 /**
  * 发布通知
  * 
  * @param c    上下文
  * @param notifyId   通知标识id
  * @param iconResId  显示的icon的id
  * @param textResId  显示的文字的id
  * @param soundResId  声音 - 没有使用(可以自己加)
  * @param titleResId  打开通知抽屉后的标题的id
  * @param contentResId  打开通知抽屉后的内容的id
  * @param cls    点击后打开的类
  * @param flag    通知标签
  * @return     返回Notification对象
  */
 static public Notification notify(Context c, int notifyId, int iconResId,
   int textResId, int soundResId, int titleResId, int contentResId,
   Class<?> cls, int flag) {
  final Resources res = ((Activity) c).getResources();

  return notify(c, notifyId, iconResId, res.getString(textResId), soundResId,
    res.getString(titleResId), res.getString(contentResId), cls,
    flag);
 }

 /**
  * 发布通知
  * 
  * @param c     上下文
  * @param notifyId    通知标识id
  * @param iconResId   显示的icon的id
  * @param notifyShowText  显示的文字
  * @param soundResId   声音 - 没有使用(可以自己加)
  * @param titleText   打开通知抽屉后的标题
  * @param contentText   打开通知抽屉后的内容
  * @param cls    点击后打开的类
  * @param flag     通知标签
  * @return      返回Notification对象
  */
 static public Notification notify(Context c, int notifyId, int iconResId,
   String notifyShowText, int soundResId, String titleText,
   String contentText, Class<?> cls, int flag) {

  Notification n = genNotification(c, notifyId, iconResId, notifyShowText, 
    soundResId, titleText, contentText, cls, flag);
  
  // 显示通知
  notify(c, notifyId, n);
  
  return n;
 }
 
 /**
  * 发布通知
  * 
  * @param c   上下文
  * @param notifyId 通知标识id
  * @param n   通知对象
  */
 static public void notify(Context c, int notifyId, Notification n) {
  final NotificationManager nm = (NotificationManager) c
    .getSystemService(Context.NOTIFICATION_SERVICE);
  
  // 显示通知
  nm.notify(notifyId, n);
 }
 
 /**
  * 生成Notification对象
  * 
  * @param c    上下文
  * @param notifyId   通知标识id
  * @param iconResId  显示的icon的id
  * @param textResId  显示的文字的id
  * @param soundResId  声音 - 没有使用(可以自己加)
  * @param titleResId  打开通知抽屉后的标题的id
  * @param contentResId  打开通知抽屉后的内容的id
  * @param cls    点击后打开的类
  * @param flag    通知标签
  * @return     返回Notification对象
  */
 static public Notification genNotification(Context c, int notifyId, int iconResId,
   int textResId, int soundResId, int titleResId, int contentResId,
   Class<?> cls, int flag) {
  final Resources res = ((Activity) c).getResources();

  return genNotification(c, notifyId, iconResId, res.getString(textResId), soundResId,
    res.getString(titleResId), res.getString(contentResId), cls,
    flag);
 }
 
 /**
  * 生成Notification对象
  * 
  * @param c     上下文
  * @param notifyId    通知标识id
  * @param iconResId   显示的icon的id
  * @param notifyShowText  显示的文字
  * @param soundResId   声音 - 没有使用(可以自己加)
  * @param titleText   打开通知抽屉后的标题
  * @param contentText   打开通知抽屉后的内容
  * @param cls    点击后打开的类
  * @param flag     通知标签
  * @return      返回Notification对象
  */
 static public Notification genNotification(Context c, int notifyId, int iconResId,
   String notifyShowText, int soundResId, String titleText,
   String contentText, Class<?> cls, int flag) {
  
  Intent intent = null;
  if (cls != null)
   intent = new Intent(c, cls);
  
  final Notification n = new Notification();

  // 控制点击通知后显示内容的类
  final PendingIntent ip = PendingIntent.getActivity(c, 
    0, // requestCode  现在是没有使用的,所以任意值都可以
    intent, 
    0 // PendingIntent的flag,在update这个通知的时候可以加特别的flag
    );
  // 设置通知图标
  n.icon = iconResId;
  // 通知文字
  n.tickerText = notifyShowText;
  // 通知发出的标志设置
  n.flags = flag;
  // 设置通知参数
  n.setLatestEventInfo(c, titleText, contentText, ip);
  
  return n;
 }

 /**
  * 取消消息
  * 
  * @param c
  * @param notifyId
  * @return void
  */
 public static void cancel(Context c, int notifyId) {
  ((NotificationManager) ((Activity) c)
    .getSystemService(Context.NOTIFICATION_SERVICE))
    .cancel(notifyId);
 }

 // flags
 final static public int FLAG_ONGOING_EVENT_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL
   | Notification.FLAG_ONGOING_EVENT;
 final static public int FLAG_ONGOING_EVENT = Notification.FLAG_ONGOING_EVENT;
 final static public int FLAG_NO_CLEAR = Notification.FLAG_NO_CLEAR;
 final static public int FLAG_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL;
}

有了这段代码,仅仅只需要简单的方法调用,就可以很方便的生成通知了。如果想在点击通知开启某个Context(Activity或者其它)时传入数据,则只需要自行处理开启用的intent即可(最好是单独使用一个方法来做,这样便不会改变上面代码对外的接口定义)。如上可见,设置了FLAG_ONGOING_EVENT这个标志之后,这个通知是“正在运行”的;FLAG_AUTO_CANCEL这个标志则是表明点击了这个通知之后,就自行的从通知栏上清除掉。

另外PendingIntent在实例的时候,代码如下: 

// 控制点击通知后显示内容的类
final PendingIntent ip = PendingIntent.getActivity(c,
 0, // requestCode  现在是没有使用的,所以任意值都可以
 intent,
 0 // PendingIntent的flag,在update这个通知的时候可以加特别的flag
);

最后一个参数表明对PendingIntent的标志位,这个标志位有时候也是很有用的,比如PendingIntent.FLAG_UPDATE_CURRENT就是声明对当前的PendingIntent(如果存在)的数据进行更新,如把intent更换了之类的,而不需要再去实例化新的对象。

类似的这些参数请自阅Android官方文档。

 Android通知的使用及简单二次封装 —— Juwend
Juwend's - http://www.juwends.com
笔者水平有限,若有错漏,欢迎指正,欢迎转载以及CV操作,但希注明出处,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值