通知的主要功能是提示功能。例如:短信、推送信息等等。
大体使用步骤:
1.获取状态通知栏管理
NotificationManager 是一个系统Service,所以必须通过getSystemService(NOTIFICATION_SERVICE)方法来获取。
|
1
2
|
notificationManager = (NotificationManager)
this
.getSystemService(NOTIFICATION_SERVICE);
|
2.实例化通知栏构造器NotificationCompat.Builder
3.设置NotificationCompat.Builder
4.设置PendingIntent
5.显示
方法或参数介绍:
1.PendingIntent
PendingIntent.getBroadcast(context, requestCode, intent, flags)
PendingIntent.getActivities(context, requestCode, intents, flags)
PendingIntent.getService(context, requestCode, intent, flags)
中的flags属性参数:
FLAG_ONE_SHOT 表示返回的PendingIntent仅能执行一次,执行完后自动取消
FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent
FLAG_UPDATE_CURRENT 表示更新的PendingIntent
2.notification.flags参数介绍
Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失
Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务
使用方法:
在设置完属性后,设置
|
1
2
|
Notification notification =builder.build();
notification.flags =Notification.FLAG_ONLY_ALERT_ONCE;
|
3.setVibrate(long[] pattern)
设置震动,需要权限.
|
1
|
<uses-permission android:name=
"android.permission.VIBRATE"
> </uses-permission>
|
4.builder.setOngoing( )
设置为ture,表示它为一个正在进行的通知。简单的说,当为ture时,不可以被侧滑消失。
***************************************************************************************
使用自定义Notification,就要使用RemoteViews。
***************************************************************************************
使用实例:
图片:

实现代码:
MainActivity.java
|
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
public
class
MainActivity
extends
Activity {
Button button, button2;
NotificationManager notificationManager;
public
final
static
String NEWS_LISTEN =
"broadcast"
;
// 用于自定义Notification,点击事件的验证
String remoteViewsText =
"未点击"
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager)
this
.getSystemService(NOTIFICATION_SERVICE);
IntentFilter filter =
new
IntentFilter();
filter.addAction(NEWS_LISTEN);
this
.registerReceiver(broadcastReceiver, filter);
}
public
void
click(View v) {
switch
(v.getId()) {
case
R.id.but:
// 使用普通的Notification
Notification.Builder builder =
new
Notification.Builder(
MainActivity.
this
);
Intent intent =
new
Intent(MainActivity.
this
, SecondActivity.
class
);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.
this
,
0
, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.close);
// 设置图标
builder.setWhen(System.currentTimeMillis());
// 设置通知来到的时间
// builder.setAutoCancel(true);
builder.setContentTitle(
"标题"
);
// 设置通知的标题
builder.setContentText(
"内容"
);
// 设置通知的内容
builder.setTicker(
"状态栏上显示"
);
// 状态栏上显示
builder.setOngoing(
true
);
/*
* // 设置声音(手机中的音频文件) String path =
* Environment.getExternalStorageDirectory() .getAbsolutePath() +
* "/Music/a.mp3"; File file = new File(path);
* builder.setSound(Uri.fromFile(file));
*/
// 获取Android多媒体库内的铃声
builder.setSound(Uri.withAppendedPath(
Audio.Media.INTERNAL_CONTENT_URI,
"5"
));
// builder.setVibrate(new long[]{2000,1000,4000}); //需要真机测试
Notification notification = builder.build();
// notification.flags =Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(
0
, notification);
break
;
case
R.id.but2:
// 使用自定义的Notification
// 3.0之前不支持Button
MyNotification();
break
;
case
R.id.but3:
// 使用下载的Notification,在4.0以后才能使用
final
Notification.Builder builder3 =
new
Notification.Builder(
MainActivity.
this
);
builder3.setSmallIcon(R.drawable.ic_launcher)
.setTicker(
"showProgressBar"
).setContentInfo(
"contentInfo"
)
.setOngoing(
true
).setContentTitle(
"ContentTitle"
)
.setContentText(
"ContentText"
);
// 模拟下载过程
new
Thread(
new
Runnable() {
@Override
public
void
run() {
int
progress =
0
;
for
(progress =
0
; progress <
100
; progress +=
5
) {
// 将setProgress的第三个参数设为true即可显示为无明确进度的进度条样式
builder3.setProgress(
100
, progress,
false
);
notificationManager.notify(
0
, builder3.build());
try
{
Thread.sleep(
1
*
1000
);
}
catch
(InterruptedException e) {
System.out.println(
"sleep failure"
);
}
}
builder3.setContentTitle(
"Download complete"
)
.setProgress(
0
,
0
,
false
).setOngoing(
false
);
notificationManager.notify(
0
, builder3.build());
}
}).start();
break
;
case
R.id.but4:
// 大布局通知在4.1以后才能使用,BigTextStyle
Notification.BigTextStyle textStyle =
new
Notification.BigTextStyle();
textStyle.setBigContentTitle(
"大标题"
)
// 标题
.setSummaryText(
"SummaryText"
)
.bigText(
"Big Text!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
+
"!!!!!!!!!!!"
+
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
);
// 内容
Notification.Builder builder2 =
new
Notification.Builder(
MainActivity.
this
);
builder2.setSmallIcon(R.drawable.icon);
// 小图标
// 大图标
builder2.setLargeIcon(BitmapFactory.decodeResource(
this
.getResources(), R.drawable.close));
builder2.setTicker(
"showBigView_Text"
)
.setContentInfo(
"contentInfo"
);
builder2.setStyle(textStyle);
builder2.setAutoCancel(
true
);
notificationManager.notify(
0
, builder2.build());
break
;
case
R.id.but5:
//大布局通知在4.1以后才能使用,大布局图片
Notification.BigPictureStyle bigPictureStyle =
new
Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.back));
Notification.Builder builder4 =
new
Notification.Builder(
MainActivity.
this
);
builder4.setSmallIcon(R.drawable.icon);
// 小图标
// 大图标
builder4.setLargeIcon(BitmapFactory.decodeResource(
this
.getResources(), R.drawable.close));
builder4.setTicker(
"showBigView_Picture"
)
.setContentInfo(
"contentInfo"
);
builder4.setStyle(bigPictureStyle);
builder4.setAutoCancel(
true
);
notificationManager.notify(
0
, builder4.build());
break
;
case
R.id.but6:
//大布局通知在4.1以后才能使用,InboxStyle
Notification.InboxStyle inboxStyle =
new
Notification.InboxStyle();
inboxStyle.setBigContentTitle(
"InboxStyle"
);
inboxStyle.setSummaryText(
"Test"
);
for
(
int
i =
0
;i<
5
;i++){
inboxStyle.addLine(
"new:"
+i);
}
Notification.Builder builder5 =
new
Notification.Builder(
MainActivity.
this
);
builder5.setSmallIcon(R.drawable.icon);
// 小图标
// 大图标
builder5.setLargeIcon(BitmapFactory.decodeResource(
this
.getResources(), R.drawable.close));
builder5.setTicker(
"showBigView_InboxStyle"
)
.setContentInfo(
"contentInfo"
);
builder5.setStyle(inboxStyle);
builder5.setAutoCancel(
true
);
notificationManager.notify(
0
, builder5.build());
break
;
}
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
// 取消广播接收
this
.unregisterReceiver(broadcastReceiver);
}
/**
* 自定义Notification
*/
public
void
MyNotification() {
RemoteViews remoteViews =
new
RemoteViews(getPackageName(),
R.layout.form);
remoteViews.setTextViewText(R.id.tv_form, remoteViewsText);
Intent intent2 =
new
Intent(MainActivity.NEWS_LISTEN);
// 使用广播,所以INTENT必须用getBroadcast方法
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(
MainActivity.
this
,
1
, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
// 绑定
remoteViews.setOnClickPendingIntent(R.id.but_form, pendingIntent2);
Notification.Builder builderMain =
new
Notification.Builder(
MainActivity.
this
);
builderMain
.setContent(remoteViews)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(
BitmapFactory.decodeResource(
this
.getResources(),
R.drawable.open)).setOngoing(
true
)
.setTicker(
"music is playing"
);
notificationManager.notify(
0
, builderMain.build());
}
// 广播接收器(自定义Notification使用到)
BroadcastReceiver broadcastReceiver =
new
BroadcastReceiver() {
@Override
public
void
onReceive(Context c, Intent intent) {
if
(intent.getAction().equals(NEWS_LISTEN)) {
remoteViewsText =
"已点击"
;
MyNotification();
}
}
};
}
|
activity_main.xml
|
1
|
<linearlayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
tools:context=
"${relativePackage}.${activityClass}"
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
><button android:id=
"@+id/but"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:onclick=
"click"
android:text=
"notification one"
></button><button android:id=
"@+id/but2"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:onclick=
"click"
android:text=
"notification two"
></button><button android:id=
"@+id/but3"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:onclick=
"click"
android:text=
"notification three"
></button><button android:id=
"@+id/but4"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:onclick=
"click"
android:text=
"notification four"
></button><button android:id=
"@+id/but5"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:onclick=
"click"
android:text=
"notification five"
></button><button android:id=
"@+id/but6"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:onclick=
"click"
android:text=
"notification six"
></button></linearlayout>
|
form.xml(自定义通知的样式)
|
1
2
3
4
5
6
7
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<linearlayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
xmlns:android=
"http://schemas.android.com/apk/res/android"
><button android:id=
"@+id/but_form"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"嘻嘻"
>
<textview android:id=
"@+id/tv_form"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:text=
"无"
>
</textview></button></linearlayout>
|
SecondActivity.java 只是一个activity。
实例的创建与设置
//api 16
PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
notification = new Notification.Builder(this)
// .addAction(icon, "拉下后的标题", p)//在这条通知下面添加一个icon图标button,点击触发p事件,例如 phone的挂断
.setContentTitle("拉下后的标题")
.setContentText("第一行内容")
// .setContent(RemoteViews)//自定义的remoteviews
// .setFullScreenIntent(p, true)//不会再通知栏直接显示,但拉下后可以显示内容 Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time.
// .setDeleteIntent(null)// 通知消失时的动作
// .setContentIntent(p)//点击的动作 相当于点击button
// .setLights(new Color().BLACK, 1000, 1000)//俺的手机不支持..无反应
// .setNumber(22)//
// .setOngoing(true)//不能被用户x掉,会一直显示,如音乐播放等
// .setPriority(2)//优先级
// .setProgress(max, progress, indeterminate)//进度条
// .setSound(uri)//声音提示
// .setSound(sound, streamType)//科设置 streamtype
// .setStyle(style)//style设置 http://developer.android.com/reference/android/app/Notification.Style.html
// .setVibrate(long[])//设置震动
.setOnlyAlertOnce(true)//一次而已
.setSmallIcon(R.drawable.page)//落下前显示的图标
.setSubText("第二行内容"+"\n"+"!!!!!!!!!!")
.setContentInfo("额外的内容")//添加到了右下角
// .setShowWhen(true)//是否允许setwhen
.setWhen(System.currentTimeMillis()+300)//何时通知 这里延迟了300ms,前提设置了setshowwher
.setTicker("直接在标题栏显示的通知", null)
.setLargeIcon(
BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher))//落下后显示的图标
.build();//生成实例 api 16后不再使用getNotification()
使用
mNotificationManager.notify(R.drawable.ic_launcher, notification);
本文详细介绍了Android中通知的实现方式,包括如何创建和显示不同类型的Notification,如普通通知、自定义视图通知、下载进度通知及大布局通知等。同时探讨了PendingIntent的作用及其常用标志符。
537

被折叠的 条评论
为什么被折叠?



