Android中的Notification
来源:互联网 发布:js控制a标签显示隐藏 编辑:程序博客网 时间:2024/06/07 23:52
Android通知Notification
Notification是一种在你APP常规UI外用来指示某个事件发生的用户交互元素。用户可以在使用其它apps时查看notification,并在方便的时候做出回应。
这里我们来一起学习通知相关知识
建立一个Notification
学习如何创建一个notification Builder,设置需要的特征,以及发布notification。
01
NotificationCompat.Builder builder =
new
NotificationCompat.Builder(
this
);
02
//设置UI界面特征
03
builder.setSmallIcon(R.drawable.m4);
//通知图标
04
builder.setContentTitle(
"通知标题"
);
05
builder.setContentText(
"1条新消息"
);
//通知内容
06
builder.setTicker(
"收到1条新消息"
);
//刚收到通知时提示
07
builder.setNumber(
1
);
//消息个数
08
builder.setAutoCancel(
true
);
// 点击取消通知
09
builder.setDefaults(Notification.DEFAULT_ALL);
// requires VIBRATE permission
10
builder.setWhen(System.currentTimeMillis());
11
builder.setSound(uri);
//通知声音
12
13
//设置行为特征
14
Intent intent =
new
Intent(
this
, BaseActivity.
class
);
15
PendingIntent pendingIntent = PendingIntent.getActivity(
this
,
0
,intent, PendingIntent.FLAG_UPDATE_CURRENT);
16
builder.setContentIntent(pendingIntent);
//通知点击行为
17
18
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
19
Notification notification = mBuilder.build();
20
int
notificationId =
10
;
//能知ID,唯一标识当前通知
21
//发布通知
22
mNotifyManager.notify(notificationId, notification);
注: pendingIntent字面意义:等待的,未决定的Intent。
要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
更新notifications
学习如何更新与移除notifications
想要设置一个可以被更新的Notification,需要在发布它的时候调用NotificationManager.notify(ID, notification))方法为它指定一个notification ID。更新一个已经发布的Notification,需要更新或者创建一个NotificationCompat.Builder对象,并从这个对象创建一个Notification对象,然后用与先前一样的ID去发布这个Notification。
移除Notification
你在创建notification时调用了 setAutoCancel(true)点击通知时移除
执行cancel(notificationId)或cancelAll()
当Activity启动时保留导航
学习如何为一个从notification启动的Activity执行适当的导航。
通知启动的是你application工作流中的一部分Activity。
步骤:
1 在manifest中定义你application的Activity层次,最终的manifest文件应该像这个:
01
<activity
02
android:name=
".MainActivity"
03
android:label=
"@string/app_name"
>
04
<intent-filter>
05
<action android:name=
"android.intent.action.MAIN"
/>
06
<category android:name=
"android.intent.category.LAUNCHER"
/>
07
</intent-filter>
08
</activity>
09
<activity
10
android:name=
".ResultActivity"
11
android:parentActivityName=
".MainActivity"
>
12
<meta-data
13
android:name=
"android.support.PARENT_ACTIVITY"
14
android:value=
".MainActivity"
/>
15
</activity>
2 在基于启动Activity的Intent中创建一个返回栈,比如:
01
int
id =
1
;
02
...
03
Intent resultIntent =
new
Intent(
this
, ResultActivity.
class
);
04
TaskStackBuilder stackBuilder = TaskStackBuilder.create(
this
);
05
// Adds the back stack
06
stackBuilder.addParentStack(ResultActivity.
class
);
07
// Adds the Intent to the top of the stack
08
stackBuilder.addNextIntent(resultIntent);
09
// Gets a PendingIntent containing the entire back stack
10
PendingIntent resultPendingIntent =
11
stackBuilder.getPendingIntent(
0
, PendingIntent.FLAG_UPDATE_CURRENT);
12
...
13
NotificationCompat.Builder builder =
new
NotificationCompat.Builder(
this
);
14
builder.setContentIntent(resultPendingIntent);
15
NotificationManager mNotificationManager =
16
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
17
mNotificationManager.notify(id, builder.build());
在notification中展示进度
学习在notification中显示某个操作的进度,既可以用于那些你可以估算已经完成多少(确定进度,determinate)的操作,也可以用于那些你无法知道完成了多少(不确定进度,indefinite )的操作
展示固定长度的进度指示器
调用NotificationCompat.Builder 的setProgress(max, progress, false))方法将进度条添加进notification,
调用setProgress(0, 0, false))方法移除进度条,
显示进度需不但改变progress值,同时更新通知
比如:
01
int
id =
1
;
02
...
03
mNotifyManager =
04
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
05
mBuilder =
new
NotificationCompat.Builder(
this
);
06
mBuilder.setContentTitle(
"Picture Download"
)
07
.setContentText(
"Download in progress"
)
08
.setSmallIcon(R.drawable.ic_notification);
09
// Start a lengthy operation in a background thread
10
new
Thread(
11
new
Runnable() {
12
@Override
13
public
void
run() {
14
int
incr;
15
// Do the "lengthy" operation 20 times
16
for
(incr =
0
; incr <=
100
; incr+=
5
) {
17
// Sets the progress indicator to a max value, the
18
// current completion percentage, and "determinate"
19
// state
20
mBuilder.setProgress(
100
, incr,
false
);
21
// Displays the progress bar for the first time.
22
mNotifyManager.notify(id, mBuilder.build());
23
// Sleeps the thread, simulating an operation
24
// that takes time
25
try
{
26
// Sleep for 5 seconds
27
Thread.sleep(
5
*
1000
);
28
}
catch
(InterruptedException e) {
29
Log.d(TAG,
"sleep failure"
);
30
}
31
}
32
// When the loop is finished, updates the notification
33
mBuilder.setContentText(
"Download complete"
)
34
// Removes the progress bar
35
.setProgress(
0
,
0
,
false
);
36
mNotifyManager.notify(id, mBuilder.build());
37
}
38
}
39
// Starts the thread by calling the run() method in its Runnable
40
).start();
创建一个自定义Notification
01
NotificationCompat.Builder mBuilder =
new
NotificationCompat.Builder(
this
);
02
mBuilder.setSmallIcon(R.drawable.m8);
03
mBuilder.setTicker(
"自定义通知,你有新消息"
);
04
mBuilder.setAutoCancel(
true
);
05
06
// ------自定义notification界面
07
RemoteViews view =
new
RemoteViews(getPackageName(),R.layout.view_notificaction_layout);
08
mBuilder.setContent(view);
09
10
// ----自定义notification事件处理
11
Intent intent =
new
Intent(
this
, RadioTabActivity.
class
);
12
PendingIntent pendingIntent = PendingIntent.getActivity(
this
,
01
,intent, PendingIntent.FLAG_UPDATE_CURRENT);
13
view.setOnClickPendingIntent(R.id.player_notification, pendingIntent);
14
15
Notification notification = mBuilder.build();
16
int
notificationId =
29
;
17
18
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
19
mNotifyManager.notify(notificationId, notification);
- Android中的Notification
- android中的Notification使用
- Android中的Notification
- Android中的Notification
- Android中的通知Notification
- Android中的Notification(通知)
- Android中的notification
- 王立平--Android中的Notification
- Android中的Notification
- Android中的通知Notification
- Android中的通知Notification
- Android中的Notification
- Android中的Notification
- android中的notification
- Android中的通知 --- Notification
- Android中的通知 Notification
- Android中的通知Notification
- android中的通知Notification
- DAPM之一:概述
- IOS-47-动态改变UIView高度最简单的方法
- eclipse进行Debug的时候,发出“java breakpoint unable to install breakpoint”错误
- Xcode 7免证书真机调试
- Appium的一点一滴:Android KEYCODE键值
- Android中的Notification
- graph slam tutorial : 从推导到应用1
- redis 订阅 发布
- 黑马52期学后总结笔记(八)
- openfire重启系统(MAC)之后无法在系统设置中打开后台管理系统
- 151228 Xcode7 本地化
- Android 解决65535的限制(官网推荐方法)
- Remove Linked List Elements 删除单链表里边指定的元素
- java7 的final真的有坑啊。