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

来源:互联网 发布:淘宝汽车冬季坐垫 编辑:程序博客网 时间:2024/06/07 07:24

文章地址:http://www.juwends.com/tech/android/android-notification.html

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * 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对象
     */
    staticpublic Notification notify(Context c, int notifyId, int iconResId,
            int textResId, int soundResId, int titleResId, int contentResId,
            Class<?> cls, int flag) {
        finalResources res = ((Activity) c).getResources();
 
        returnnotify(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对象
     */
    staticpublic 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);
         
        returnn;
    }
     
    /**
     * 发布通知
     *
     * @param c         上下文
     * @param notifyId  通知标识id
     * @param n         通知对象
     */
    staticpublic void notify(Context c, int notifyId, Notification n) {
        finalNotificationManager 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对象
     */
    staticpublic Notification genNotification(Context c, int notifyId, int iconResId,
            int textResId, int soundResId, int titleResId, int contentResId,
            Class<?> cls, int flag) {
        finalResources res = ((Activity) c).getResources();
 
        returngenNotification(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对象
     */
    staticpublic 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);
         
        finalNotification n = newNotification();
 
        // 控制点击通知后显示内容的类
        finalPendingIntent 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);
         
        returnn;
    }
 
    /**
     * 取消消息
     *
     * @param c
     * @param notifyId
     * @return void
     */
    publicstatic void cancel(Context c, int notifyId) {
        ((NotificationManager) ((Activity) c)
                .getSystemService(Context.NOTIFICATION_SERVICE))
                .cancel(notifyId);
    }
 
    // flags
    finalstatic public int FLAG_ONGOING_EVENT_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL
            | Notification.FLAG_ONGOING_EVENT;
    finalstatic public int FLAG_ONGOING_EVENT = Notification.FLAG_ONGOING_EVENT;
    finalstatic public int FLAG_NO_CLEAR = Notification.FLAG_NO_CLEAR;
    finalstatic public int FLAG_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL;
}

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

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

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

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


0 0
原创粉丝点击