Android实现3种Notification(状态栏通知)

点击标题下「安卓干货铺」可快速关注

Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当 APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。下面会分别实现普通的通知,带自定义视图的通知,还有悬挂似的通知。

3种方式开始前都要先执行下面这行代码:

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

图片一.普通通知:

效果图:

图片

创建Builder对象,利用PendingIntent来跳转,然后给Builder添加各种属性。

完整的代码如下:

Notification.Builder builder=new Notification.Builder(this);

Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));

PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

builder.setContentIntent(pendingIntent);

builder.setSmallIcon(R.mipmap.ic_launcher);

builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

builder.setAutoCancel(true);

builder.setContentTitle("普通通知");

mNotificationManager.notify(1, builder.build());

图片二.带视图的通知

效果图:

图片

自定义视图有两种状态,一种是上面的效果图,即展开状态的视图;另一种是普通状态下的视图,需要我们手动的拉一下,折叠部分才会出来。我们需要用到RemoteViews哎创建视图。

视图的布局我就不贴出来了,就是个普通的水平布局。

指定展开状态的视图:

notification.bigContentView=remoteViews;

普通状态视图:

notification.contentView=remoteViews;

完整代码如下:

Notification.Builder builder2=new Notification.Builder(this);

Intent intent2=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));

PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,intent2,0);

builder2.setContentIntent(pendingIntent2);

builder2.setSmallIcon(R.mipmap.ic_launcher);

builder2.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

builder2.setAutoCancel(true);

builder2.setContentTitle("折叠通知");

RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_view);

Notification notification=builder2.build();

notification.bigContentView=remoteViews;

mNotificationManager.notify(1,notification);

图片三.悬挂式的通知

效果图:

图片

悬挂式是Android5.0以后的新特性,前两种需要手动拉通知栏,而这种方式不需要,直接就可以显示在屏幕上方。

完整代码如下:

Notification.Builder builder3=new Notification.Builder(this);

Intent intent3=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));

PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,intent3,0);

builder3.setContentIntent(pendingIntent3);

builder3.setSmallIcon(R.mipmap.ic_launcher);

builder3.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

builder3.setAutoCancel(true);

builder3.setContentTitle("悬挂通知");

Intent XuanIntent=new Intent();

XuanIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

XuanIntent.setClass(this,MainActivity.class);

PendingIntent xuanpengdIntent=PendingIntent.getActivity(this,0,XuanIntent,PendingIntent.FLAG_CANCEL_CURRENT);

builder3.setFullScreenIntent(xuanpengdIntent,true);

mNotificationManager.notify(2,builder3.build());

附加:Android5.0以后可以设置通知的等级

VISIBILITY_PUBLIC: 任何情况的显示

VISIBILITY_PRIVATE: 只有在没有锁屏时显示

VISIBILITY_SECRET: 在安全锁下或者没锁屏下显示

Android5.0以后可以通过builder.setVisibility(Notification.VISIBILITY_PUBLIC);设置。

源码下载:

http://download.csdn.net/download/qq_34908107/9898690

Android状态栏语言,Android实现3种Notification(状态栏通知)相关推荐

  1. android 初始化语言,Android Init Language(安卓初始化语言)

    android源码学习目录 了解Android init进程的都知道,启动 init进程之前会解析init.rc文件,init.rc是Android的配置文件脚本,它由一种被称为"Andro ...

  2. android添加语言,android如何添加一种新语言

    前些时候,因为客户要求加波斯语,但是目前android2.3.1并没有这种语言,在客户要求一定要有波斯语的情况,经过长时间的摸索与在网上求助,终于将波斯语给加出来了. 下面讲讲新增加一种语言的步骤: ...

  3. android 阿拉伯语言,android - 阿拉伯语言的语音识别

    我正在开发一个语音识别系统,我用英语制作了一个,它运作得很好 . 但我遇到的问题是我需要开发应用程序以识别阿拉伯语 . 这是我的代码: private static final int RESULT_ ...

  4. android默认语言,Android默认设置App内的语言为中文

    1.通用的设置App语言的方法(需要注意版本的变化) public static Context initAppLanguage(Context context, String language) { ...

  5. android自定义语言,Android 修改App语言

    定义资源文件 在res目录下创建 values 默认 values-en 英文 values-zh 中文 分别存放不同的语言资源 保存语言设置 可以用SharePreference保存语言设置 pub ...

  6. android 初始化语言,25.Android init language (安卓初始化语言)

    目前在研究framework框架,AIL是Android初始化语言,以下是根据官方文档进行的翻译有助于研究Android启动过程: Android初始化语言包含了四种类型的声明:Actions(行动) ...

  7. Android初始化语言 (init.*.rc、init.conf文件格式)

    2019独角兽企业重金招聘Python工程师标准>>> 下文转载自:http://hi.baidu.com/zhlg_hzh/blog/item/4ad24a807c71d3db90 ...

  8. android 初始化语言,3.4.1 Android初始化语言(1)

    3.4 init.rc文件解析过程 init.rc文件按照一定的格式组织,要分析init.rc文件的解析过程,首先要熟悉它的格式.init.rc的文件格式由Android初始化语言(Android I ...

  9. android insmod命令,android的启动脚本(init.rc)文件的语法

    android的启动脚本(init.rc)文件的语法,参考资料: Android 初始化语言(Android Init Language) Android初始化脚本语言包含四种类型的语句: 动作(Ac ...

最新文章

  1. Windows phone 7新开发工具发布
  2. Jenkins中的邮件设置
  3. python怎么打开shell界面-使用IDLE的Python shell窗口实例详解
  4. SDN和SD-WAN有本质区别—Vecloud微云
  5. linux 扫描仪 程序,Linux桌面的4种扫描工具
  6. 交叉编译成windows版
  7. Git 的安装、使用、分支、命令 一篇博客全都学会
  8. iPhone 13 Pro Max Magsafe保护壳曝光:命名方式疑似确认
  9. Bit.com BCH期权上线以来日交易量持续翻倍
  10. SpringBoot2.x炫酷吊炸天前后端分离的后台管理系统实例
  11. 将路由器作为AP来使用
  12. 【编程语言】Swift完全自学手册
  13. 已解决ImportError: Pandas requires version ‘2.0.1‘ or newer of ‘xlrd‘ (version ‘1.2.0‘ currently instal
  14. 剑指offe【31-67】
  15. 2022高频面试题之css篇
  16. Angular5 + Bootstrap4使用示例
  17. 基于Xilinx artix 7的FPGA高级应用(二):千兆以太网通信(原理篇)
  18. DNF2020年全新脚本展示第二部分
  19. ​2022年云市场营收排行榜
  20. 2021.5.22 Python爬虫实战:效率办公,从下载文献开始...

热门文章

  1. U-mail邮件系统对故障的紧急措施有哪些?
  2. 用python监控您的window服务
  3. 在IT界取得成功应该知道的10件事(ZT)
  4. Windows Workflow HOL学习笔记(十二):创建状态基工作流
  5. eight_queens
  6. python电视剧口碑分析_小案例(七):口碑分析(python)
  7. java服务发现_【Java】Eureka – 服务发现(Server)
  8. qwidget show 是否有信号_c++笔记(信号集处理函数)
  9. 西南科技大学计算机专业宿舍条件怎么样,西南科技大学宿舍条件,宿舍环境图片(10篇)...
  10. java web listener_JavaWeb:Listener