This commit is contained in:
chenjuntao
2018-10-28 14:14:56 +08:00
parent 74c9cfc5b6
commit 26bc1299ff
5 changed files with 45 additions and 15 deletions

View File

@ -1,10 +1,12 @@
package com.gh.download;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import com.gh.common.util.AppDebugConfig;
@ -28,8 +30,9 @@ public class DownloadNotification {
public static final String ACTION_INSTALL = "com.gh.gamecenter.INSTALL";
public static final String ACTION_DOWNLOAD = "com.gh.gamecenter.DOWNLOAD";
public static final int ICON = R.drawable.logo;
private static final int NOTIFY_ID = 0x123;
private static final int WAIT_NOTIFY_ID = 0x124;
public static final int NOTIFY_ID = 0x123;
public static final int WAIT_NOTIFY_ID = 0x124;
private static final String CHANNEL_ID = "Halo_Download";
private static NotificationManager getNotificationManager(Context context) {
return (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
@ -38,6 +41,12 @@ public class DownloadNotification {
//TODO 下载重构
public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity) {
final NotificationManager manager = getNotificationManager(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
}
Intent intent = new Intent();
intent.putExtra(EntranceUtils.KEY_PATH, downloadEntity.getPath());
intent.setAction(ACTION_INSTALL);
@ -58,7 +67,8 @@ public class DownloadNotification {
}
title = "下载完成,点击立即安装";
}
final Notification notification = new NotificationCompat.Builder(context)
final Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.logo)
.setTicker(title)
.setContentTitle(text)
@ -85,7 +95,11 @@ public class DownloadNotification {
downloadingSize++;
}
}
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager nManager = getNotificationManager(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
nManager.createNotificationChannel(channel);
}
if (downloadingSize == 0) {
nManager.cancel(NOTIFY_ID);
} else {
@ -93,7 +107,7 @@ public class DownloadNotification {
intent.setAction(ACTION_DOWNLOAD);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(ICON)
.setTicker("点击查看")
.setContentTitle("正在下载" + downloadingSize + "个游戏")
@ -110,12 +124,16 @@ public class DownloadNotification {
AppDebugConfig.logMethodWithParams(DownloadNotification.class, context);
}
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager nManager = getNotificationManager(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
nManager.createNotificationChannel(channel);
}
Intent intent = new Intent();
intent.setAction(ACTION_DOWNLOAD);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(ICON)
.setTicker("连接WiFi后自动恢复下载")
.setContentTitle("下载已暂停")