Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7093839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:29:22+00:00 2026-05-28T08:29:22+00:00

Hello i have class for download file. He work as expected except for remove

  • 0

Hello i have class for download file. He work as expected except for remove notification when download is finished. I note what two identical sections of code in effect give different stuff. I mean now I have following behavior: I start download file and I can cancel them by click on notification. Download canceled as expected, the one that must be abort. But if wait until download will finish. Second notification will abort when first will finish and first notification will be stay in notification bar forever. I don’t know where my mistake.
Example code:

public class DownloadVkVideoFiles extends AsyncTask<String, Integer, String>{
    private static String BROADCAST_ACTION = "com.yourshows.helper.DownloadVkVideoFile.CANCELID";

    public DownloadVkVideoFiles(Context c, String title, int taskId) {
    this.context = c;
    this.notifyId = taskId; //this is unique notification Id
    this.BROADCAST_ACTION += String.valueOf(taskId); //broadcast action for pending intent
    }

   @Override
   protected void onPreExecute() {
        // execute the status bar notification
        createNotification();
        super.onPreExecute();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        context.registerReceiver(receiver, filter); //register Receiver for cancel download file
     }

    @Override
    protected String doInBackground(String... params) { //download file}

    @Override
    public void onProgressUpdate(Integer... progress) {
         notification.contentView.setProgressBar(R.id.progressBar, 100, progress[0], false);
         // inform the progress bar of updates in progress
         notificationManager.notify(notifyId, notification);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        notificationManager.cancel(notifyId); // close finished notification
        unregisterReceiver();
        LOG.send(LOG.I, TAG, "Notification finished with ID = " + notifyId);
    }

    @Override
     protected void onCancelled() {
        super.onCancelled();
        notificationManager.cancel(notifyId);
        unregisterReceiver();

        LOG.send(LOG.I, TAG, "Notification finished with ID = " + notifyId);
    }
}

UPD:
createNotification:

    public void createNotification() {
         notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
         Intent notificationIntent = new Intent();
         notificationIntent.setAction(BROADCAST_ACTION);

         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

         RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.download_notification);

         // TODO change to shows title
         tickerText = context.getResources().getText(R.string.downloadTitle);
         icon = android.R.drawable.stat_sys_download;
         time = System.currentTimeMillis();

         notification = new Notification(icon, tickerText, time);
         notification.flags |= Notification.FLAG_ONGOING_EVENT;
         notification.flags |= Notification.FLAG_AUTO_CANCEL;
         notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

         contentView.setImageViewResource(R.id.downloadImage, R.drawable.download);
         contentView.setTextColor(R.id.title, notification_text_color);
         contentView.setFloat(R.id.title, "setTextSize", notification_text_size - 3);
         contentView.setTextViewText(R.id.title, title);
         contentView.setProgressBar(R.id.progressBar, 100, 0, false);

         notification.contentIntent = pendingIntent;
         notification.contentView = contentView;
        //notificationManager.notify(notifyId, notification);
         service.startForeground(notifyId, notification);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:29:23+00:00Added an answer on May 28, 2026 at 8:29 am

    I tried something like that in my project but if you try to download more than 5 files the the download files will behave unexpected the all files damaged.
    so i change the structure to Service instead of AsyncTask.
    downloads stored in queue in download one by one the notification bar contain Progress bar number of download and current file name and download percentage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ruby file as follows: module Example class Myclass def t_st Hello
I have a file called hellowf.cs class MyFirstApp { static void Main() { System.Windows.Forms.MessageBox.Show(Hello,
Hello i did my notification of download file from web. Code below. I want
say I have: class Test { public static int Hello = 5; } This
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
I have the following form code: # forms.py class SomeForm(forms.Form): hello = forms.CharField(max_length=40) world
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
Lets say I have this code: <?php class hello { var $greeting = hello;
-- Hello, just ast the title says. I have a downloader class that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.