近期一直在研究 android 。并一边研究一边做应用。当中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ一样)的问题。经过研究实现了其功能。现把 Notification 的使用总结例如以下:

Notification 的使用须要导入 3 个类

1
2
3
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;

代码演示样例及说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NotificationManager
nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);              
Notification
n =
new Notification(R.drawable.chat,
"Hello,there!",
System.currentTimeMillis());            
n.flags
= Notification.FLAG_AUTO_CANCEL;               
Intent
i =
new Intent(arg0.getContext(),
NotificationShow.
class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);          
//PendingIntent
PendingIntent
contentIntent = PendingIntent.getActivity(
        arg0.getContext(),
        R.string.app_name,
        i,
        PendingIntent.FLAG_UPDATE_CURRENT);
                 
n.setLatestEventInfo(
        arg0.getContext(),
        "Hello,there!",
        "Hello,there,I'm
john."
,
        contentIntent);
nm.notify(R.string.app_name,
n);

以下依次对每一段代码进行分析:

1
NotificationManager
nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

创建 NotificationManager。当中创建的 nm 对象负责“发出”与“取消”  Notification。

1
2
Notification
n =
new Notification(R.drawable.chat,
"Hello,there!",
System.currentTimeMillis());            
n.flags
= Notification.FLAG_ONGOING_EVENT; 

创建 Notification ,參数依次为:icon的资源id,在状态栏上展示的滚动信息。时间。当中创建的 n 对象用来描写叙述出如今系统通知栏的信息。之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。

1
n.flags
= Notification.FLAG_AUTO_CANCEL;

设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后。可以清除该通知。

1
2
Intent
i =
new Intent(arg0.getContext(),
NotificationShow.
class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent

请注意,假设要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。

Intent.FLAG_ACTIVITY_CLEAR_TOP :假设在当前Task中,有要启动的Activity。那么把该Acitivity之前的全部Activity都关掉,并把此Activity置前以避免创建Activity的实例

Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前全部已创建的Task中是否有该要启动的Activity的Task,若有。则在该Task上创建Activity,若没有则新建具有该Activity属性的Task。并在该新建的Task上创建Activity。很多其它请參见 “ (转载)Android下Affinities和Task ”

1
2
3
4
5
6
//PendingIntent
PendingIntent
contentIntent = PendingIntent.getActivity(
        arg0.getContext(),
        R.string.app_name,
        i,
        PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent 为Intent的包装。这里是启动Intent的描写叙述。PendingIntent.getActivity 返回的PendingIntent表示。此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的參数依次为:Context。发送者的请求码(能够填0),用于系统发送的Intent,标志位。

当中 PendingIntent.FLAG_UPDATE_CURRENT  表示假设该描写叙述的PendingIntent已存在。则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。

这里再简要说一下 Intent 与 PendingIntent 的差别:

Intent :意图,即告诉系统我要干什么。然后系统依据这个Intent做相应的事。如startActivity相当于发送消息,而Intent是消息的内容。

PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此假设我们不用某种方法来告诉系统的话。系统是不知道是使用
startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其它的“描写叙述”),因此这里便须要PendingIntent。

1
2
3
4
5
n.setLatestEventInfo(
        arg0.getContext(),
        "Hello,there!",
        "Hello,there,I'm
john."
,
        contentIntent);

设置显示在通知下拉框中的信息,參数依次为:Context,标题。内容,PendingIntent。

1
nm.notify(R.string.app_name,
n);

启动Notification,參数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation。假设程序中仅仅有一个Notification那么这里随便你填什么都能够,只是类型必需要为int),要通知的Notification。

怎样使自己的Notification像Android QQ一样能出如今 “正在执行的”栏目以下

事实上非常easy,仅仅需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便能够了。

怎样改变 Notification 在“正在执行的”栏目以下的布局

创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便能够了。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
PendingIntent
contentIntent = PendingIntent.getActivity(
    arg0.getContext(),
    R.string.app_name,
    i,
    PendingIntent.FLAG_UPDATE_CURRENT);
             
RemoteViews
rv =
new RemoteViews(Main.this.getPackageName(),
R.layout.notification_view);
rv.setImageViewResource(R.id.image,
R.drawable.chat);
rv.setTextViewText(R.id.text,
"Hello,there,I'm
john."
);
n.contentView
= rv;
n.contentIntent
= contentIntent;
 
nm.notify(R.string.app_name,
n);

注意,假设使用了contentView。那么便不要使用Notification.setLatestEventInfo。假设setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖。显示的便是 setLatestEventInfo 的效果。假设 setLatestEventInfo
在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说无论你想要setLatestEventInfo 或 contentView 的自己定义效果,请保证始终仅仅有一句设置代码,由于在最后一句绑定的时候。之前的设置contentView或setLatestEventInfo的代码都是全然没有必要的。

android Notification 的使用(锁定通知栏)的更多相关文章

  1. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  2. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  3. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  4. Android Notification和权限机制探讨

    近期为了在部门内做一次小型的技术分享.深入了解了一下Notification的实现原理.以及android的权限机制.在此做个记录.文章可能比較长,没耐心的话就直接看题纲吧. 先看一下以下两张图 图一 ...

  5. Android Notification通知简介

    Android Notification通知简介 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面 ...

  6. Android Notification 消息通知 相关资料.md

    目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...

  7. 3、android notification 详细用法

    在 android 系统中,在应用程序可能会遇到几种情况需要通知用户,有的需要用户回应,有的则不需要,例如: * 当保存文件等事件完成,应该会出现一个小的消息,以确认保存成功. * 如果应用程序在后台 ...

  8. android notification 传值关键

    android notification 传值关键在 onNewIntent方法里获取 @Override protected void onCreate(Bundle savedInstanceSt ...

  9. Android NOtification 使用(震动 闪屏 铃声)

    一. Notification 简介 在 android 系统中,在应用程序可能会遇到几种情况需要通知用户,有的需要用户回应,有的则不需要,例如: * 当保存文件等事件完成,应该会出现一个小的消息,以 ...

  10. Android Notification实现推送消息过程中接受到消息端有声音及震动及亮屏提示

    在Android Notification状态栏通知一文中,简单实现了消息的推送效果,这里就接着上文说一下,当用户接受到消息时的提示效果 // 5-增加震动及声音及亮屏 notification.de ...

随机推荐

  1. MS SQL数据批量备份还原(适用于MS SQL 2005+)

    原文:MS SQL数据批量备份还原(适用于MS SQL 2005+) 我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较 ...

  2. 【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】

    方法1:通过get()方法获取属性值 package com.sxd.test.controller; public class FirstCa{ private Integer num; priva ...

  3. iOS开发之字符串比较

    Object-c中比较两个字符串是否相等时,应该用isEqualToString:而不能仅仅只是比较字符串的指针值. NSString *str1=@"hello 1";  NSS ...

  4. JAVA常见算法题(十九)

    package com.xiaowu.demo; /** * * 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. * * * @author WQ ...

  5. [Android Traffic] 调整定时更新的频率(C2DM与退避算法)

    转载自: http://blog.csdn.net/kesenhoo/article/details/7395253 Minimizing the Effect of Regular Updates[ ...

  6. docker下载ubuntu并进行修改后生成新的镜像提交

    一  docker pull ubuntu ,先下载下来一个镜像, 或者 从本地启动一个镜像 docker run -i -t ubuntu /bin/bash 二 进入一定更新操作 # shell ...

  7. 转:敏捷方式scrum 方案

    http://www.cnblogs.com/taven/archive/2010/10/17/1853386.html 现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... ...

  8. 要做国外的app,使用到的分享和统计SDK推荐

    国内的就不说了,多如牛毛,常用的是友盟,极光,shareSDK等等. 国外的统计有: Flurry(https://developer.yahoo.com) google analytics(http ...

  9. Laravel的本地化

    一.简介 Laravel 的本地化功能提供方便的方法来获取多语言的字符串.语言包存放在 resources/lang 文件夹的文件里.在此文件夹内应该有网站对应支持的语言并将其对应到每一个子目录: / ...

  10. 10分钟-jQuery-基础选择器

    1.id 选择器 jquery能使用CSS选择器来操作网页中的标签元素.假设你想要通过一个id号去查找一个元素,就能够使用例如以下格式的选择器: $("#my_id") 当中#my ...