android - postexecute() 异步类中的进度条问题
问题描述
当用户从应用程序下载内容时,我在通知中显示进度条,一切正常,唯一的问题是下载完成后程序进入 onPostExecute() 函数但进度条没有被删除,我使用 Log 对其进行了测试.d() 进行跟踪。任何想法 -
protected void onPreExecute() {
super.onPreExecute();
mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Uri selectedUri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
mNotifyManager.createNotificationChannel(notificationChannel);
mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
mBuilder.setContentTitle("Downloading - "+title)
.setContentText("Download in progress")
.setSmallIcon(R.drawable.doupnowlogo)
.setOnlyAlertOnce(true)
.setProgress(100, 0, false)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setContentIntent(pendingIntent);
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
else
{
mBuilderOld = new NotificationCompat.Builder(context, null);
mBuilderOld.setContentTitle("Downloading - "+title)
.setContentText("Download in progress")
.setSmallIcon(R.drawable.doupnowlogo)
.setOnlyAlertOnce(true)
.setProgress(100, 0, false)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
mNotifyManager.notify(2, mBuilderOld.build());
}
//Toast.makeText(context, "Downloading file...", Toast.LENGTH_SHORT).show();
}
protected void onProgressUpdate(Integer... progress) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setProgress(100, progress[0], false);
// Displays the progress bar on notification
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
else
{
mBuilderOld.setProgress(100, progress[0], false);
// Displays the progress bar on notification
mNotifyManager.notify(2, mBuilderOld.build());
}
Log.d("Progress","I am here");
super.onProgressUpdate(progress);
}
protected void onPostExecute(Void result){
super.onPostExecute(result);
super.onProgressUpdate(100);
Log.d("Progress","I am done");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
else
{
mBuilderOld.setContentText("Download complete");
// Removes the progress bar
mBuilderOld.setProgress(0, 0, false);
mNotifyManager.notify(2, mBuilderOld.build());
}
}
解决方案
推荐阅读
- c# - 如何在 .Net core 2.1 的渐进式 Web 应用程序中存储数据?
- javascript - 使用 vis.js 从 json 文件中填充节点和链接
- android - 如何在应用更新中为 android 配置灵活或立即发布?
- python - 有没有办法通过只知道href来点击按钮?没有 id 或 value 可以清楚地区分。在 python 中使用 Selenium
- asp.net - 通过控制输入的值
- javascript - 在函数参数中传递用户定义的类型
- azure - 有没有办法在创建 Azure 容器实例时设置没有值的环境变量?
- azure - 在arm模板中传递除参数文件之外的其他参数
- angular - 如何根据条件禁用或阻止打开 wijmo 树节点?
- php - 通过php查找和替换字符串中的Img标签?