private NotificationManager notificationManager;
private NotificationCompat.Builder builder;
private NotificationClickReceiver notificationClickReceiver;
public class DownloadManager {
private static final String TAG = "DownloadManager";
private Context mContext;
private String downloadUrl;
private String savePath;
private DownloadRecord downloadRecord;
/**
* 下载
*
* @param context 上下文
* @param downloadUrl 下载地址
* @param savePath 下载后保存到本地的路径
*/
public void download(Context context, String downloadUrl, String savePath) {
this.mContext = context;
this.downloadUrl = downloadUrl;
this.savePath = savePath;
try {
downloadRecord = Downloader.createRecord(downloadUrl, savePath,
new DownloadListenerAdapter() {
@Override
public void onTaskStart(DownloadRecord record) {
Log.d(TAG, "onTaskStart");
initNotification();
}
public void onTaskPause(DownloadRecord record) {
Log.d(TAG, "onTaskPause");
}
public void onTaskCancel(DownloadRecord record) {
Log.d(TAG, "onTaskCancel");
}
public void onProgressChanged(DownloadRecord record, int progress) {
Log.d(TAG, "onProgressChanged progress=" + progress);
updateNotification(progress);
}
@Override
public void onTaskSuccess(DownloadRecord record) {
Log.d(TAG, "onTaskSuccess");
showInstall();
}
@Override
public void onTaskFailure(DownloadRecord record, Throwable throwable) {
Log.e(TAG, "onTaskFailure error=" + throwable);
updateNotification(-1);
}
});
} catch (Exception x) {
Log.e(TAG, "download error=" + x);
}
}
/**
* 初始化通知
*/
private void initNotification() {
try {
notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Android8.0及以后的方式
if (Build.VERSION.SDK_INT >= 26) {
// 创建通知渠道
NotificationChannel notificationChannel = new NotificationChannel("download_channel", "下载",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(false); //关闭闪光灯
notificationChannel.enableVibration(false); //关闭震动
notificationChannel.setSound(null, null); //设置静音
notificationManager.createNotificationChannel(notificationChannel);
}
builder = new NotificationCompat.Builder(mContext, "download_channel");
builder.setContentTitle("已下载(0%)") //设置标题
.setSmallIcon(mContext.getApplicationInfo().icon) //设置小图标
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
mContext.getApplicationInfo().icon)) //设置大图标
.setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级
.setAutoCancel(false) //设置通知被点击一次不自动取消
.setSound(null) //设置静音
.setContentText("正在下载 点击取消") //设置内容
.setProgress(100, 0, false) //设置进度条
.setContentIntent(createIntent()); //设置点击事件
notificationManager.notify(100, builder.build());
} catch (Exception x) {
Log.e(TAG, "initNotification error=" + x);
}
}
/**
* 刷新通知
*
* @param progress 百分比,此值小于0时不刷新进度条
*/
private void updateNotification(int progress) {
if (builder == null) {
return;
}
if (progress >= 0) {
builder.setContentTitle("已下载(" + progress + "%)");
builder.setProgress(100, progress, false);
}
if (downloadRecord == null || downloadRecord.getState() == DownloadRecord.STATE_FAILURE) {
builder.setContentText("下载失败 点击重试");
} else if (progress == 100) {
builder.setContentText("下载完成 点击安装");
builder.setAutoCancel(true);
}
notificationManager.notify(100, builder.build());
}
/**
* 设置通知点击事件
*
* @return 点击事件
*/
private PendingIntent createIntent() {
Intent intent = new Intent(mContext.getPackageName() + ".upgrade.notification");
intent.setPackage(mContext.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
/**
* 注册通知点击监听
*/
private void registerReceiver() {
notificationClickReceiver = new NotificationClickReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(mContext.getPackageName() + ".upgrade.notification");
mContext.registerReceiver(notificationClickReceiver, intentFilter);
}
/**
* 处理通知栏点击事件
*/
public class NotificationClickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (!(mContext.getPackageName() + ".upgrade.notification").equals(action)) {
return;
}
if (downloadRecord != null) {
int state = downloadRecord.getState();
Log.d(TAG, "onReceive state=" + state);
switch (state) {
case DownloadRecord.STATE_CREATE:
case DownloadRecord.STATE_PENDING:
case DownloadRecord.STATE_RUNNING:
case DownloadRecord.STATE_PAUSE:
// 关闭通知栏
notificationManager.cancel(100);
break;
case DownloadRecord.STATE_SUCCESS:
// 显示安装确认弹窗
showInstallAlert(true);
break;
case DownloadRecord.STATE_FAILURE:
// 重新下载
download(mContext, this.downloadUrl, this.savePath);
break;
default:
break;
}
}
}
}
}
注:本文主要介绍通知栏中如何显示进度条,对于文中的下载逻辑以及下载状态等(Downloader、DownloadRecord、DownloadListenerAdapter)仅列出框架,并不进行深入讲解,因为这是另一个话题。
|