android Notification 简单工具类
程序员文章站
2022-07-13 14:33:25
...
非常简单的一个通知栏工具类,用在项目中需要修改下。平时没事写个demo用还可以。实现了最新android8.0通知栏方式
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("下载完成","下载完成!");
上一篇: Android通知的使用你可能会遇到的坑
推荐阅读
-
Android顶部工具栏和底部工具栏的简单实现代码
-
Android动画工具类的封装实战记录
-
Android开发中总结的Adapter工具类【附完整源码下载】
-
Android开发之图片压缩工具类完整实例
-
Android开发实现的获取sdcard大小及内存大小工具类
-
Android实现下载工具的简单代码
-
Android开发实现的几何图形工具类GeometryUtil完整实例
-
Android开发实现的IntentUtil跳转多功能工具类【包含视频、音频、图片、摄像头等操作功能】
-
Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
-
Android开发之拼音转换工具类PinyinUtils示例