KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am using this example to download a file from a server using AsycTask and to show the downlaod progress in a notification bar. I just modified the doInBackground method in order to downoad my file: @Override protected Void doInBackground(String... Urls) { //This is where we would do the actual download stuff //for now I'm just going to loop for 10 seconds // publishing progress every second try { URL url = new URL(Urls[0]); URLConnection connection = url.openConnection(); connection.connect(); // this will be useful so that you can show a typical 0-100% // progress bar int fileLength = connection.getContentLength(); // download the file InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream( _context.getFilesDir() + "/file_name.apk"); byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count ; // publishing the progress.... publishProgress((int) (total * 100 / fileLength)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e){ e.printStackTrace(); } return null; } protected void onPreExecute() { // Create the notification in the statusbar mNotificationHelper.createNotification(); } protected void onPostExecute(Void result) { // The task is complete, tell the status bar about it mNotification
Tags (comma-separated)
Save Edits
Cancel