1.Notification创建

   首先,介绍一下,创建一个通知所需要用到的类和方法

   

NotificationManager类

NotificationManager类是用来管理系统的所有通知的类,该类的对象必须通过Context类的getSystemService()方法获取。完整代码:

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

  notify()作用是告知系统显示该通知,有notify (int id, Notification notification)和notify (String tag,int id, Notification

  notification),id表示通知的id,tag表示通知的标志,主要用于区分各个通知,notification指的是通知对象;

 cancel(int id) 表示移除指定id的通知,cancel(String tag,int id)移除指定Id和tag的通知,cancelAll()移除所有通知。

  Notification类

   notification有一些常用的属性:

    icon 设置通知图标(在API23后使用setSmallIcon(Icon)替代)

    number 通知所显示的事件数量,例如,收到邮件通知,则指的是邮件未读数量(这是用API11创建的通知所表现的作用)。如果通知是用Notification.builder创建,则number表示扩展通知视图,为0或者负数的时候,通知不显示。

   tickerText 通知显示在通知栏的文本,只在通知栏上显示一次。

   when 系统当前时间

   flags 取值有:

                FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应

   
    defaults 设置默认值

DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUND 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动

  contentView 拉下通知栏后,通知条显示视图,类型是RemoteView;


 contentIntent 点击通知条控件时,响应的意图


   一些常用的方法

    构造方法:

     public Notification (int icon, CharSequence tickerText, long when),如果使用属性的方式设置这些值,那也可以使用无参构造函数
  
  在API11之后使用Notification.builder()创建

   setLatestEventInfo(Context context,CharSequence title, CharSequence content, PendingIntent intent);
             本方法用于显示通知栏下拉后,通知条的内容。

 PendingIntent类
PendingIntent这个类用于处理即将发生的事情。
该对象的获取方式为, PendingIntent.getActivity(Context context,int requestCode,Intent intent,int flags);requsetCode和flags一般默认设置为0;
下面用上面提到的知识,写一个简单的通知(基于API11之前):
public void showBaseNotification() {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification notify = new Notification();notify.icon = R.drawable.ic_launcher;notify.tickerText = "您有新短消息,请注意查收!";notify.when = System.currentTimeMillis();PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);notify.setLatestEventInfo(this, "Notification Title","This is the notification message", pendingIntent);notify.number = 1;notify.flags |= Notification.FLAG_AUTO_CANCEL; // 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示manager.notify(1, notify);}

基于API11之后:

 public void showNotification() {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder nb = new NotificationCompat.Builder(getApplicationContext()).setContentIntent(PendingIntent.getActivity(MainActivity.this, 0,new Intent(this, MainActivity.class),PendingIntent.FLAG_UPDATE_CURRENT)).setAutoCancel(true).setContentTitle("test title").setContentText("message").setSmallIcon(R.drawable.ic_launcher).setLights(Color.RED, 600, 1000).setVibrate(new long[] { 0, 200, 300, 500 }).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));manager.notify(1, nb.build());}

   

Android之Notification初识相关推荐

  1. android之Notification通知

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...

  2. Android关于notification的在不同API下的用法说明

    当我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们 ...

  3. Android 通知栏Notification

    Android 通知栏Notification 在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态 ...

  4. android activity 被notification启动,Android通知Notification全面剖析

    原标题:Android通知Notification全面剖析 通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通 ...

  5. Android之Notification制作多媒体控制器

    上一篇讲述了Notification的基础用法,本篇将介绍,自定义通知栏,并利用讲到的内容,实现一个简单的音乐播发器. 1.自定义通知的实现: Notification有一个contentView属性 ...

  6. Android Train—notification通知

    Notification extends Object implements Parcelable java.lang.Object ↳ android.app.Notification Public ...

  7. Android笔记 notification

    notification 通知 即在通知栏发消息,例如短信到来,发现软件更新都可以在手机上方的通知栏看见,下拉屏幕即可看见通知 1布局 <RelativeLayout xmlns:android ...

  8. Android通知栏Notification弹出横幅显示的解决方法

    Android通知栏Notification弹出横幅显示的解决方法 参考文章: (1)Android通知栏Notification弹出横幅显示的解决方法 (2)https://www.cnblogs. ...

  9. Android Q notification创建发送流程-framework篇

    基于Android10源码分析notification创建到添加到systemui的流程 本篇主要分析framework部分 以下是发送一个简单notification的示例代码: public st ...

最新文章

  1. python requests 报错 Connection aborted ConnectionResetError RemoteDisconnected 解决方法
  2. 【响应式Web前端设计】CSS浮动(float,clear)讲解
  3. junit4 assert类中的assert方法总结
  4. hdu 2795(线段树)
  5. 错误消息 parent.relativePath points at wrong local POM的处理方法
  6. 装修月记第一弹,硬装篇
  7. 为此计算机上的所有用户安装此加载项,activex 安装给所有计算机用户
  8. Guns 删除功能_入门试炼07
  9. Flutter实战一Flutter聊天应用(四)
  10. CSS 3D透视效果 星空穿越
  11. java题库管理系统java试题管理系统java考试管理系统
  12. javaWeb新闻管理系统
  13. 学习笔记15--车道线检测
  14. MFI认证——什么是苹果MFI认证
  15. AS移动开发 类微信界面2_Activity的生命周期与跳转(持续更新中)
  16. c语言编程存款问题,c程序问题输入存款金额money存期yea...
  17. (翻译)测试替身— Fakes, Mocks 和 Stubs
  18. Android笔记:浅析Android电视APP开发
  19. ASP.NET防注入
  20. android书籍推荐!分析Android未来几年的发展前景,灵魂拷问

热门文章

  1. 前端学习(1731):前端系列javascript之发布窗口布局下
  2. 前端学习(1720):前端系列javascript之生命周期下
  3. 第二十四期:揭秘:为什么电脑越用越卡 大型破案现场
  4. html:(39):块级元素和内联块级元素
  5. html:(34):下划线和删除线
  6. ARM 移植 PPPD
  7. js获取当前时间(昨天、今天、明天)
  8. linux mysql5.7.11_在Linux中以命令行方式安装 MySQL 5.7.11 for Linux Generic 二进制版本
  9. assert函数_PHP 之 assert()函数
  10. oracle导出审计表,Oracle审计表AUD$处理方法