zoukankan      html  css  js  c++  java
  • 跟我学android-Notification

    Notification 可以理解为通知的意思,会出现在通知栏,比如来了一条短信

    使用 Notification 有以下3个步骤:

    1. 创建 NotificationManager的对象

    2.为Notification设置属性

    3.使用 NotificationManager 提供的 notify 发送通知

    实例:发出一个通知

     1     /**
     2      * 创建notify
     3      */
     4     private void createNotify() {
     5         // 创建NotificationManager 对象
     6         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     7         // 创建notifyCation对象
     8         Notification notify = new Notification();
     9         notify.icon = R.drawable.icon_reply;//设置图标
    10         notify.when = System.currentTimeMillis();//发通知的时间,立即
    11         notify.tickerText = "hi,我来了";//提示文字
    12         notify.flags = Notification.FLAG_AUTO_CANCEL;//用户点击后 自动取消
    13         Intent intent = new Intent(this, NextActivity.class);
    14         PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
    15                 PendingIntent.FLAG_UPDATE_CURRENT);
    16         notify.setLatestEventInfo(this, "来消息啦", "一条通知", pIntent);
    17         manager.notify(10, notify);//发出通知,10是 通知的id
    18     }

    这个方法 大家可以设置是 按钮的事件里 调用,运行程序后,点击按钮 就可以看到通知发送出来了。 布局文件 和相关的代码 这里就不在编写。

    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。

  • 相关阅读:
    80X86常用汇编指令集(不断补充完善中)
    重温大师经典:Martin Fowler 的持续集成
    .NET Framework 将有限地向开发者公开源代码
    DotNet源代码中的模式Builder生成器模式
    Delphi内嵌汇编语言BASM精要(转帖)
    sealed关键字用法
    VS2008设置查看.NET源码的方法
    基于LINQ to SQL的WEB开发三层架构(1)
    拓扑排序
    HDU1495 非常可乐(BFS)
  • 原文地址:https://www.cnblogs.com/blog-IT/p/3981697.html
Copyright © 2011-2022 走看看