1、统一下载逻辑,下一步准备移动到通用类库
2、修复viewimageactivity npe
This commit is contained in:
111
app/src/main/java/com/gh/download/DownloadNotification.java
Normal file
111
app/src/main/java/com/gh/download/DownloadNotification.java
Normal file
@ -0,0 +1,111 @@
|
||||
package com.gh.download;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import com.gh.common.util.PlatformUtils;
|
||||
import com.gh.gamecenter.R;
|
||||
|
||||
|
||||
/**
|
||||
* @author CsHeng
|
||||
* @Date 26/06/2017
|
||||
* @Time 6:01 PM
|
||||
*/
|
||||
|
||||
public class DownloadNotification {
|
||||
|
||||
private static final int NOTIFY_ID = 0x123;
|
||||
|
||||
// public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity, int flag) {
|
||||
// NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
// Intent intent = new Intent();
|
||||
// intent.putExtra("path", downloadEntity.getPath());
|
||||
// intent.setAction(DownloadConfig.ACTION_INSTALL);
|
||||
// PendingIntent pendingIntent = PendingIntent.getBroadcast(context, flag,
|
||||
// intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
// Notification notification = new NotificationCompat.Builder(context)
|
||||
// .setSmallIcon(DownloadConfig.ICON)
|
||||
// .setTicker("点击安装")
|
||||
// .setContentTitle(downloadEntity.getName() + "下载完成")
|
||||
// .setContentText("点击安装")
|
||||
// .setContentIntent(pendingIntent).build();
|
||||
//// notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
|
||||
// notification.flags |= Notification.FLAG_AUTO_CANCEL; // // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
|
||||
// nManager.notify(flag, notification);
|
||||
// }
|
||||
|
||||
private static NotificationManager getNotificationManager(Context context) {
|
||||
return (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
//TODO 下载重构
|
||||
public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity, int flag) {
|
||||
NotificationManager manager = getNotificationManager(context);
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("path", downloadEntity.getPath());
|
||||
intent.setAction(DownloadConfig.ACTION_INSTALL);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, flag,
|
||||
intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
String text;
|
||||
String title;
|
||||
if (downloadEntity.isPluggable()) {
|
||||
title = "下载完成,点击继续插件化";
|
||||
text = downloadEntity.getName() + " - "
|
||||
+ PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform());
|
||||
} else {
|
||||
if (downloadEntity.isPlugin()) {
|
||||
text = downloadEntity.getName()
|
||||
+ " - " + PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform());
|
||||
} else {
|
||||
text = downloadEntity.getName();
|
||||
}
|
||||
title = "下载完成,点击立即安装";
|
||||
}
|
||||
Notification notification = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.logo)
|
||||
.setTicker(title)
|
||||
.setContentTitle(text)
|
||||
.setContentText(title)
|
||||
.setContentIntent(pendingIntent).build();
|
||||
// notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
|
||||
notification.flags |= Notification.FLAG_AUTO_CANCEL; // // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
|
||||
manager.notify(flag, notification);
|
||||
}
|
||||
|
||||
public static void showDownloadingNotification(Context context) {
|
||||
int downloadingSize = 0;
|
||||
for (DownloadEntity entity : DownloadManager.getInstance(context).getAll()) {
|
||||
if (entity.getStatus().equals(DownloadStatus.downloading)
|
||||
|| entity.getStatus().equals(DownloadStatus.waiting)
|
||||
|| entity.getStatus().equals(DownloadStatus.pause)
|
||||
|| entity.getStatus().equals(DownloadStatus.timeout)
|
||||
|| entity.getStatus().equals(DownloadStatus.neterror)) {
|
||||
downloadingSize++;
|
||||
}
|
||||
}
|
||||
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
if (downloadingSize == 0) {
|
||||
nManager.cancel(NOTIFY_ID);
|
||||
} else {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(DownloadConfig.ACTION_DOWNLOAD);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID,
|
||||
intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
Notification notification = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(DownloadConfig.ICON)
|
||||
.setTicker("点击查看")
|
||||
.setContentTitle("正在下载" + downloadingSize + "个游戏")
|
||||
.setContentText("点击查看")
|
||||
.setContentIntent(pendingIntent).build();
|
||||
// notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
|
||||
notification.flags |= Notification.FLAG_NO_CLEAR; // 通知无法手动清除
|
||||
nManager.notify(NOTIFY_ID, notification);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user