读书笔记 多媒体(一)——通知notification

Notification

使用场景:当应用程序希望向用户发出一些提示msg,而应用程序又不在前台运行时,通过通知来实现。通知算是Android中比较常用的一个功能,可以保持自己App的长存,在用户没有进入App的时候,也提供了与用户交互的可能。

思维导图结构

思维导图的大体结构(按照各个节点延伸拓展学习)

Notificaiton – service – BroadcastReceiver – Intent(flag、Action等属性应用) – PendingIntent

感慨:
一个Notificaiton通知的拓展使用就要涉及与4大组建的配合,所以学好整体的知识体系。
联系:

1.由于service 是在后台运行,所以它意图做什么我们看不到,可以通过Notificaiton 来显示提醒(如音乐的后台播放)。

2.service服务和BroadcastReceiver广播相结合,在加上Notificaiton 显示(如程序的后台更新)。

3.Intent作为意图处理,和Notificaiton的点击时间紧密结合在了一起,并且与BroadcastReceiver和service的联系也紧密不可以分割。
(service 在后台之后通过BroadcastReceiver来通知Notificaiton 显示相关东西,在通过Intent完成用户的意图操作)

相关文档:Activity启动模式 及 Intent Flags 与 栈 的关联分析

基本用法

创建

可以在活动、广播和服务中创建,但在后两者中比较经常。(前台后台)
1、NotificationManager获取
用Context中的getSysTemService(服务名)来获取,接收一个字符串用于确定获取系统哪个服务。

NotificationManager manager = (NotificationManager )getSystemService(Context.NOTIFICATION_SERVICE);

2、Notification对象

NotificationCompat.Builder.build()来获得notification对象自己

一个notification对象需要包含如下内容:(最少)
小图标(setSmallIcon()获取)
标题(setContentTitle()获取)
详情文字(setContentText()获取)

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
mBuilder.setContentTitle("测试标题")//设置通知栏标题  
    .setContentText("测试内容") /<span style="font-family: Arial;">/设置通知栏显示内容</span>  
    .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  
//  .setNumber(number) //设置通知集合的数量  
    .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的  
    .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间  
    .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  
//  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消    
    .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)  
    .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合  
    //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission  
    .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON  

3、调用notify()发送通知
notify()接收两个参数(id, notification);注意,id和通知是唯一绑定的关系。

manager.notify(1, notification);

notification更新

当你需要为同一类型事件多次发送一个通知,要避免完全的重新创建一个notification。考虑更新之前的通知,要么修改或者增加它的一些值,或者两者都。

修改通知

To set up a notification so it can be updated, issue it with a notification ID by callingNotificationManager.notify(ID, notification). To update this notification once you’ve issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously。
其实,意思就是:直接再重新创建或者去更新builder(比如用之前的builder去调用设置函数来修改),然后用这个创建一个notification,在notify()发送和需要更新的通知一样的id.

//不仅仅是有更新,还有固定持续时间进度条
int id = 1;
...
mNotifyManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            // Do the "lengthy" operation 20 times
            for (incr = 0; incr <= 100; incr+=5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(id, mBuilder.build());
                        // Sleeps the thread, simulating an operation
                        // that takes time
                        try {
                            // Sleep for 5 seconds
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) {
                            Log.d(TAG, "sleep failure");
                        }
            }
            // When the loop is finished, updates the notification
            mBuilder.setContentText("Download complete")
            // Removes the progress ba
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值