• Android控件之Notification


      Android通知就是让设备在屏幕最顶上那栏里面显示图标,当滑下通知栏之后可以看到列表状的通知选项,有些是“通知”类型的,有些是“正在运行”类型的,“通知”类型的通知是可以清除的,“正在运行”类型的通知是无法清除的,比如短信来了,顶上的状态栏就会出现通知,这么通知通常是可以被清除掉的,还比如听音乐的时候出现的通知,这么通知通常就不能清除的、正在运行的类型,具体如何定义这两种类型将会在后面的代码中给出。

       参考这位朋友实例 感觉写着很轻便也很清晰。

    转载自:https://www.juwends.com/tech/android/android-notification.html

    /*
     * Copyright (C) 2013 Juwend's Demo
     *
     * 本代码可以任意复制与改动,欢迎转载,转载请注明出处
     *
     *
     */
     
    package com.juwends.helper;
     
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.Resources;
     
    /**
     * 通知制造
     *
     * @author Juwend
     *
     */
    public class NotificationHelper {
        // 1.实例化Notification类
        // 2.设置Notification对象的icon,通知文字,声音
        // 3.实例化PendingIntent类,作为控制点击通知后显示内容的对象
        // 4.加载PendingIntent对象到Notification对象(设置 打开通知抽屉后的 标题/内容)
        // 5.获得 NotificationManager对象
        // 6.使用NotificationManager对象显示通知
     
        /**
         * 发布通知
         *
         * @param c             上下文
         * @param notifyId      通知标识id
         * @param iconResId     显示的icon的id
         * @param textResId     显示的文字的id
         * @param soundResId    声音 - 没有使用(可以自己加)
         * @param titleResId    打开通知抽屉后的标题的id
         * @param contentResId  打开通知抽屉后的内容的id
         * @param cls           点击后打开的类
         * @param flag          通知标签
         * @return              返回Notification对象
         */
        static public Notification notify(Context c, int notifyId, int iconResId,
                int textResId, int soundResId, int titleResId, int contentResId,
                Class<?> cls, int flag) {
            final Resources res = ((Activity) c).getResources();
     
            return notify(c, notifyId, iconResId, res.getString(textResId), soundResId,
                    res.getString(titleResId), res.getString(contentResId), cls,
                    flag);
        }
     
        /**
         * 发布通知
         *
         * @param c                 上下文
         * @param notifyId          通知标识id
         * @param iconResId         显示的icon的id
         * @param notifyShowText    显示的文字
         * @param soundResId        声音 - 没有使用(可以自己加)
         * @param titleText         打开通知抽屉后的标题
         * @param contentText       打开通知抽屉后的内容
         * @param cls               点击后打开的类
         * @param flag              通知标签
         * @return                  返回Notification对象
         */
        static public Notification notify(Context c, int notifyId, int iconResId,
                String notifyShowText, int soundResId, String titleText,
                String contentText, Class<?> cls, int flag) {
     
            Notification n = genNotification(c, notifyId, iconResId, notifyShowText,
                    soundResId, titleText, contentText, cls, flag);
     
            // 显示通知
            notify(c, notifyId, n);
     
            return n;
        }
     
        /**
         * 发布通知
         *
         * @param c         上下文
         * @param notifyId  通知标识id
         * @param n         通知对象
         */
        static public void notify(Context c, int notifyId, Notification n) {
            final NotificationManager nm = (NotificationManager) c
                    .getSystemService(Context.NOTIFICATION_SERVICE);
     
            // 显示通知
            nm.notify(notifyId, n);
        }
     
        /**
         * 生成Notification对象
         *
         * @param c             上下文
         * @param notifyId      通知标识id
         * @param iconResId     显示的icon的id
         * @param textResId     显示的文字的id
         * @param soundResId    声音 - 没有使用(可以自己加)
         * @param titleResId    打开通知抽屉后的标题的id
         * @param contentResId  打开通知抽屉后的内容的id
         * @param cls           点击后打开的类
         * @param flag          通知标签
         * @return              返回Notification对象
         */
        static public Notification genNotification(Context c, int notifyId, int iconResId,
                int textResId, int soundResId, int titleResId, int contentResId,
                Class<?> cls, int flag) {
            final Resources res = ((Activity) c).getResources();
     
            return genNotification(c, notifyId, iconResId, res.getString(textResId), soundResId,
                    res.getString(titleResId), res.getString(contentResId), cls,
                    flag);
        }
     
        /**
         * 生成Notification对象
         *
         * @param c                 上下文
         * @param notifyId          通知标识id
         * @param iconResId         显示的icon的id
         * @param notifyShowText    显示的文字
         * @param soundResId        声音 - 没有使用(可以自己加)
         * @param titleText         打开通知抽屉后的标题
         * @param contentText       打开通知抽屉后的内容
         * @param cls               点击后打开的类
         * @param flag              通知标签
         * @return                  返回Notification对象
         */
        static public Notification genNotification(Context c, int notifyId, int iconResId,
                String notifyShowText, int soundResId, String titleText,
                String contentText, Class<?> cls, int flag) {
     
            Intent intent = null;
            if (cls != null)
                intent = new Intent(c, cls);
     
            final Notification n = new Notification();
     
            // 控制点击通知后显示内容的类
            final PendingIntent ip = PendingIntent.getActivity(c,0,  // requestCode  现在是没有使用的,所以任意值都可以
                    intent,
                    // PendingIntent的flag,在update这个通知的时候可以加特别的flag
                    );
            // 设置通知图标
            n.icon = iconResId;
            // 通知文字
            n.tickerText = notifyShowText;
            // 通知发出的标志设置
            n.flags = flag;
            // 设置通知参数
            n.setLatestEventInfo(c, titleText, contentText, ip);
     
            return n;
        }
     
        /**
         * 取消消息
         *
         * @param c
         * @param notifyId
         * @return void
         */
        public static void cancel(Context c, int notifyId) {
            ((NotificationManager) ((Activity) c)
                    .getSystemService(Context.NOTIFICATION_SERVICE))
                    .cancel(notifyId);
        }
     
        // flags
        final static public int FLAG_ONGOING_EVENT_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONGOING_EVENT;
        final static public int FLAG_ONGOING_EVENT = Notification.FLAG_ONGOING_EVENT;
        final static public int FLAG_NO_CLEAR = Notification.FLAG_NO_CLEAR;
        final static public int FLAG_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL;
    }

      有了这段代码,仅仅只需要简单的方法调用,就可以很方便的生成通知了。如果想在点击通知开启某个Context(Activity或者其它)时传入数据,则只需要自行处理开启用的intent即可(最好是单独使用一个方法来做,这样便不会改变上面代码对外的接口定义)。如上可见,设置了FLAG_ONGOING_EVENT这个标志之后,这个通知是“正在运行”的;FLAG_AUTO_CANCEL这个标志则是表明点击了这个通知之后,就自行的从通知栏上清除掉。

      最后一个参数表明对PendingIntent的标志位,这个标志位有时候也是很有用的,比如PendingIntent.FLAG_UPDATE_CURRENT就是声明对当前的PendingIntent(如果存在)的数据进行更新,如把intent更换了之类的,而不需要再去实例化新的对象。

  • 相关阅读:
    预习非数值数据的编码方式
    预习原码补码
    C语言||作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言|作业12—学期总结
    C语言|博客作业11
    第三章预习
  • 原文地址:https://www.cnblogs.com/cold-ice/p/6067942.html
Copyright © 2020-2023  润新知