SoFunction
Updated on 2025-03-11

Android download file notification bar displays the instance code of progress bar function

1. Use AsyncTask asynchronous task implementation, call publishProgress() method to refresh the progress to achieve (optimized)

public class MyAsyncTask extends AsyncTask<String,Integer,Integer> {
  private Context context;
  private NotificationManager notificationManager;
  private  builder;
  public MyAsyncTask(Context context){
     = context;
    notificationManager = (NotificationManager) (Activity.NOTIFICATION_SERVICE);
    builder = new (context);
  }
  @Override
  protected void onPreExecute() {
    ();
    (.ic_launcher)
        .setContentInfo("Download...")
        .setContentTitle("Downloading");
  }
  @Override
  protected Integer doInBackground(String... params) {
    (TAG, "doInBackground: "+params[0] );
    InputStream is = null;
    OutputStream os = null;
    HttpURLConnection connection = null;
    int total_length = 0;
    try {
      URL url1 = new URL(params[0]);
      connection = (HttpURLConnection) ();
      ("GET");
      (50000);
      ();
      if(() == 200){
        is = ();
        os = new FileOutputStream("/sdcard/");
        byte [] buf = new byte[1024];
        int len;
        int pro1=0;
        int pro2=0;
        // Get the file stream size for updating progress        long file_length = ();
        while((len = (buf))!=-1){
          total_length += len;
          if(file_length>0) {
            pro1 = (int) ((total_length / (float) file_length) * 100);//Passing progress (note the order)          }
          if(pro1!=pro2) {
            // Call the update function to update the progress            publishProgress(pro2=pro1);
          }
          (buf, 0, len);
        }
      }
    } catch (MalformedURLException e) {
      ();
    } catch (IOException e) {
      ();
    }finally {
      try {
        if (is != null) {
          ();
        }
        if (os != null) {
          ();
        }
      } catch (IOException e) {
        ();
      }
      if (connection != null) {
        ();
      }
    }
    return total_length;
  }
  @Override
  protected void onCancelled(Integer integer) {
    (integer);
  }
  @Override
  protected void onCancelled() {
    ();
  }
  @Override
  protected void onProgressUpdate(Integer... values) {
    (values);
    (100,values[0],false);
    (0x3,());
    //Download progress prompt    ("download"+values[0]+"%");
    if(values[0]==100) {  //Click to install after downloading      Intent it = new Intent(Intent.ACTION_VIEW);
      (Intent.FLAG_ACTIVITY_NEW_TASK);
      (("file:///sdcard/"), "application/-archive");
      PendingIntent pendingIntent = (context, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
      ("Download Completed")
          .setContentText("Click to install")
          .setContentInfo("Download Completed")
          .setContentIntent(pendingIntent);
      (0x3, ());
    }
  }
  @Override
  protected void onPostExecute(Integer integer) {
    (integer);
    if(integer == 100) {
      (context, "Download Completed", Toast.LENGTH_SHORT).show();
    }
  }
}

2. Use system services to implement it (this method is not particularly recommended)

//Get the system download service    DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    //Create a download request object     request=new ((downUrl));
    ("Table of contents","file name");
    (.NETWORK_MOBILE|.NETWORK_WIFI);
    (.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    (request);

Summarize

The above is the example code for the Android download file notification bar display progress bar function introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!