zoukankan      html  css  js  c++  java
  • Notification封装(没做从网络下载)

    1.Notification作为消息通知,这里简单封装了使用

    建立一个bean

    package com.jcut.view;
    
    /**
     * Author:JsonLu
     * DateTime:2016/2/26 14:25
     * Email:jsonlu@qq.com
     * Desc:
     **/
    public class NotificationModel {
    
    
        private String NTitle;
        private String NContent;
        private long NWhen;
        private String NTicker;
        private int NType;
        private String NIcon;
    
        public String getNTitle() {
            return NTitle;
        }
    
        public void setNTitle(String NTitle) {
            this.NTitle = NTitle;
        }
    
        public String getNContent() {
            return NContent;
        }
    
        public void setNContent(String NContent) {
            this.NContent = NContent;
        }
    
        public long getNWhen() {
            return NWhen;
        }
    
        public void setNWhen(long NWhen) {
            this.NWhen = NWhen;
        }
    
        public String getNTicker() {
            return NTicker;
        }
    
        public void setNTicker(String NTicker) {
            this.NTicker = NTicker;
        }
    
        public int getNType() {
            return NType;
        }
    
        public void setNType(int NType) {
            this.NType = NType;
        }
    
        public String getNIcon() {
            return NIcon;
        }
    
        public void setNIcon(String NIcon) {
            this.NIcon = NIcon;
        }
    }

    简单的Notification 的封装

    package com.jcut.view;
    
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.support.v7.app.NotificationCompat;
    
    import com.example.jcut.R;
    
    /**
     * Author:JsonLu
     * DateTime:2016/2/26 12:46
     * Email:jsonlu@qq.com
     * Desc:
     **/
    public class Notification extends NotificationCompat {
    
        private Builder mBuilder;
    
        public Notification(Context context) {
            mBuilder = new Builder(context);
        }
    
        public Notification(Context context, NotificationModel model) {
            mBuilder = new Builder(context);
            mBuilder.setContentTitle(model.getNTitle())
                    .setContentText(model.getNContent())
                    .setTicker(model.getNTicker())
                    .setWhen(model.getNWhen())
                    .setPriority(PRIORITY_DEFAULT)
                    .setOngoing(false)
                    .setDefaults(model.getNType())
                    .setSmallIcon(R.drawable.ic_launcher);//此处要下载图片到本地
        }
    
        public Builder getmBuilder() {
            return mBuilder;
        }
    
        public void setContentIntent(CallBack call) {
            mBuilder.setContentIntent(call.getPendingIntent());
        }
    
        public void notify(int notifyId, NotificationManager manager) {
            android.app.Notification notify = mBuilder.build();
            notify.flags = Notification.FLAG_AUTO_CANCEL;
            manager.notify(notifyId, notify);
        }
    
    
        public interface CallBack {
            PendingIntent getPendingIntent();
        }
    }

    简单的Test

    public void showButtonNotify() {
            Notification customNot = new Notification(this);
            NotificationCompat.Builder mBuilder = customNot.getmBuilder();
            mBuilder.setContentTitle("测试标题")//设置通知栏标题
                    .setContentText("测试内容") //设置通知栏显示内容
                    .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的
                    .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                    .setPriority(PRIORITY_DEFAULT) //设置该通知优先级
                    .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                    .setDefaults(DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
    //                        Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
                    .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON
    
            customNot.setContentIntent(new CallBackImpl());
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            customNot.notify(1, mNotificationManager);
        }
    
        public PendingIntent getDefalutIntent(int flags) {
            Intent notificationIntent = new Intent(this, TestActivity.class);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            return contentIntent;
        }
    
        class CallBackImpl implements Notification.CallBack {
            @Override
            public PendingIntent getPendingIntent() {
                return getDefalutIntent(1);
            }
        }
  • 相关阅读:
    show proceslist时发现大量的sleep,有什么风险吗,该如何处理?
    监控MySQL的性能,应该主要观察那几个监控项?
    MySQL所有的压力都在一个CPU核心上,为什么会产生这种现象,改如何解决?
    大表,某列无索引,先需要查询该列,删除符合条件的记录,大约占40%数据量,请问有何更好的方案吗?
    MySQL DBA运维中那些动作属于危险性操作?
    云环境上自建MySQL,有哪些高可用实现方案?
    RDS上,MySQL实例中某张表数据小于tmp_table_size,但有查询时会报错临时空间满 The table '/data/mysql/zst/tmp/#sql_13975_23' is full. 原因可能是什么?
    MySQL误删除frm文件该怎么办?
    生产环境MySQL死锁如何监控及如何减少死锁发生的概率。
    MongoDB有哪些优秀特性及适合的场景是什么?
  • 原文地址:https://www.cnblogs.com/Jsonlu/p/5221094.html
Copyright © 2011-2022 走看看