首页 > 解决方案 > 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());
            }
   }

标签: android

解决方案


推荐阅读