zoukankan      html  css  js  c++  java
  • Android 通知的基本用法

    一。概念

      通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望像用户发送一些提示消息的时候,然而此时应用程序并不在前台运行,此时就可以借助通知来实现。发出一条通知后,手机的最上方的状态栏会显示一个通知的图标,下拉状态栏之后可以看到通知的详细信息。

    二。创建通知的步骤

      1. 首先需要一个  NotificationManager  来对通知你进行管理。

        可以调用  Context  的getSystemService() 方法来得到  NotificationManager 对象。 getSystemService()方法接收一个参数就可以确定获取系统的哪个服务。在这里我们传入Context.NOTIFICATION_SERVICE 即可。

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

      2. 接下来需要创建一个 Notification 对象,这个对象用于存储通知所需的各种信息,我们可以使用它的有三个参数的构造函数来进行创建。

        Notification()  Constructs a Notification object with default values.

        Notification(int icon, CharSequence tickerText, long when)   This constructor was deprecated      in API level 11.    Use Notification.Builder instead.

        Notification(Parcel parcel)   Unflatten the notification from a parcel.

        我们可以使用Notification的有有三个参数的造函数。

        第一个参数用于指定通知的图标,比如项目的 res/drawable 目录下有一张 icon.png 图片,那么这里我们传入 R.drawable.icon。

        第二个参数用于指定通知的 ticker 内容,当通知刚被创建是,他会在系统状态栏一闪而过,属于一种瞬时提示消息。

        第三个参数用于指定通知被创建的时间,当下拉状态栏时,这里指定的时间会显示在通知上。

        所以,我们创建一个 Notification 对象就可以写成:

    Notification notification = new Notification(R.drawable.icon,  "This is ticker text", System.currentTimeMellis());

      3. 创建好了 Notification 对象之后我们还需要对通知的布局进行设定。

        我们只需要调用 setLatestEventInfo() 方法就可以给通知设置一个标准的布局。

        public  void  setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)

        这个方法接收四个参数:

        第一个是 Context;第二个用于指定标题内容下拉状态栏是,就是现实这个内容第三个用于指定正文内容,同样是下拉状态栏的时候可以看到,第四个参数 我们先传入null,后面再说,这个参数是用于指定,点击这个通知的时候的响应;

        

    notification.setLatestEventInfo(context, "This is content title", "This is content", null);

      4. 最后我们只需要 NotificationManager 的 notify() 方法就可以让通知显示出来。

        public  void  notify(int id, Notification notification)

        public  void  notify(String tag, int id, Notification notification)

        notify 方法接收两个参数: 第一个是 id,要保证每个通知指定的 id 是唯一的; 第二个参数则是 Notification 对象,这里我们只需要把刚才创建好的 notification 对象传入即可。

    manager.notify(1, notification);

    到这里我们就完成了通知的创建,通知既可以在活动中创建,还可以在广播接收器和服务中创建。我们一般不在活动中创建,不管在哪儿创建,步骤都是一样的。

      5. 下拉状态栏,响应点击通知栏。

        3 中的 setLatestEventInfo 的最后一个参数 PendingIntent 就是在合适的时机去执行某个动作。

        可以根据你创建 通知的地方调用相应的方法得到 PendingIntent 的实例, getActivity(),getBroadcast() , getService()。

        这几个方法接收的参数是相同的,第一个是 Context,第二个一般用不到,传入0即可,第三个参数是 Intent ,通过这个实现跳转, 第四个参数用于确定PendingInetent 的行为。

    Intent intent = new Intent(this, NotificationActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    notification.setLatestEventInfo(context, "This is content title", "This is content", pendingIntent);

       6. 跳转到对应的界面后,取消通知栏

      

      

    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(1);
  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/kest/p/5058885.html
Copyright © 2011-2022 走看看