import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.ContextWrapper;import android.content.Intent;import android.os.Build;import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationManagerCompat;public class NotificationUtils extends ContextWrapper {    private int notifyId = 1;    private String channelId = "1";    private int smallIcon;    private String title;    private String content;    private NotificationCompat.Builder builder;    private NotificationManager notificationManager;    private NotificationManagerCompat notificationManagerCompat;    public NotificationUtils(Context context, int smallIcon, String title, String content) {        this(context, 1, null, smallIcon, title, content);    }    public NotificationUtils(Context context, int notifyId, int smallIcon, String title, String content) {        this(context, notifyId, null, smallIcon, title, content);    }    public NotificationUtils(Context context, int notifyId, String channelId, int smallIcon, String title, String content) {        super(context);        this.notifyId = notifyId;        this.channelId = channelId != null ? channelId : this.notifyId+"";        this.smallIcon = smallIcon;        this.title = title;        this.content = content;        baseNotification();    }    public void notifyProgress(int max, int progress, String title, String content) {        if (builder != null && progress > 0) {            builder.setContentTitle(title);            builder.setContentText(content);            builder.setProgress(max, progress, false);            notify();        }    }    public void completeProgress(String title, String content) {        notifyProgress(0, 0, title, content);    }    public void notifyed() {        notify(builder);    }    public void notify(Intent intent) {        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);        notify(builder.setContentIntent(pendingIntent));    }    private void notify(NotificationCompat.Builder builder) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            getNotificationManager();            notificationManager.notify(notifyId, builder.build());        } else {            getNotificationManagerCompat();            notificationManagerCompat.notify(notifyId, builder.build());        }    }    public void cancel(int notifyId) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            notificationManager.cancel(notifyId);        } else {            notificationManagerCompat.cancel(notifyId);        }    }    private void baseNotification() {        builder = getBuilder(getApplicationContext(), channelId);        builder.setSmallIcon(smallIcon);        builder.setContentTitle(title);        builder.setContentText(content);    }    private NotificationCompat.Builder getBuilder(Context context, String channelId) {        return (builder = new NotificationCompat.Builder(context, channelId));    }    private NotificationCompat.Builder getBuilder(Context context) {        return (builder = new NotificationCompat.Builder(context));    }    private void getNotificationManagerCompat() {        notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());    }    private void getNotificationManager() {        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        int importance = NotificationManager.IMPORTANCE_DEFAULT;        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationChannel channel = new NotificationChannel(channelId, "channel_name", importance);            notificationManager.createNotificationChannel(channel);        }    }    public NotificationManager getManager() {        return this.notificationManager;    }    public NotificationCompat.Builder getBuilder() {        return builder;    }}

 

//普通通知栏消息NotificationUtils notificationUtils = new NotificationUtils(this, R.mipmap.ic_launcher, "title", "content......");notificationUtils.notifyed();//notificationUtils.cancel(1);//进度通知栏notificationUtils.notifyProgress(100, progress += 10, "正在下载", progress + "%");notificationUtils.completeProgress("下载完成","下载完成!");

 

更多相关文章

  1. Android的ProgressBar以及自定义进度条
  2. Android最好看的圆形进度条ProgressBar
  3. 多线程实现更新android进度条。
  4. Android 各种自定义进度条Progressbar
  5. Android自带Music播放器更新播放时间和进度条的方法
  6. Android 5.1系统禁止通知状态栏下拉
  7. Android 下载文件至SD卡,并用progressBar显示下载进度
  8. Android 创建悬停通知栏
  9. Android下Notification(通知栏)的使用

随机推荐

  1. 学习PHP中使用Session配合Javascript实现
  2. 探索PHP 生命周期
  3. 探索php+ajax实现带进度条的大数据排队导
  4. PHP如何自定义的 printf 函数
  5. php上传多张图片时,选择图片后即可预览的
  6. php session不过期的实现方法
  7. 详解PHP中错误与异常及其相关知识
  8. php工厂方法模式是什么
  9. 浅谈CGI、FastCGI、PHP-CGI、PHP-FPM!
  10. php如何安装zip模块?(方法介绍)