Android 进度条通知栏通知
public class MyNotificationUtils {
private static NotificationManager manager;
private static NotificationManager getManager(Context context) {
if (manager == null) {
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
return manager;
}
private static NotificationCompat.Builder getNotificationBuilder(Context mContext,String title
, String content, String channelId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId
, mContext.getPackageName(), NotificationManager.IMPORTANCE_DEFAULT);
channel.canBypassDnd();
channel.enableLights(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
channel.setLightColor(Color.RED);
channel.canShowBadge();
channel.enableVibration(true);
channel.setSound(null, null);
channel.getGroup();
channel.setBypassDnd(true);
channel.setVibrationPattern(new long[]{100, 100, 200});
channel.shouldShowLights();
getManager(mContext).createNotificationChannel(channel);
}
return new NotificationCompat.Builder(mContext,channelId).setAutoCancel(true)
.setContentTitle(title)
.setContentText(content).setSmallIcon(R.mipmap.ic_launcher);
}
public static void showNotificationProgress(Context mContext,String title
, String content, int manageId, String channelId
, int progress, int maxProgress) {
final NotificationCompat.Builder builder = getNotificationBuilder(mContext,title,content,channelId);
builder.setOnlyAlertOnce(true);
builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
builder.setProgress(maxProgress, progress, false);
builder.setWhen(System.currentTimeMillis());
getManager(mContext).notify(manageId, builder.build());
}
public static void showNotificationProgressApkDown(Context mContext
, int progress) {
final NotificationCompat.Builder builder = getNotificationBuilder(mContext,"正在下载","悠游云驾","yunjia");
builder.setOnlyAlertOnce(true);
builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
builder.setProgress(100, progress, false);
builder.setWhen(System.currentTimeMillis());
getManager(mContext).notify(R.drawable.ic_launcher, builder.build());
}
public static void cancleNotification(Context mContext,int manageId) {
getManager(mContext).cancel(manageId);
}