Updating the Progressbar in the notification bar using AsyncTask in a service?

I am trying to download several binaries using a Service that contains AsynTask for background work. I can successfully start the service, I registered it in the manifest file. I can upload files, however I would like to show a progress bar in the notification bar. I create a notification panel inside the onPreExecute () method and set the progress panel and notifying NotifactionManager inside the onProgressUpdate () method.

private class DownloadFileAsync extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notification = new Notification(R.drawable.icon, "Downloading...", System.currentTimeMillis()); contentView = new RemoteViews(getPackageName(), R.layout.progress_layout); contentView.setProgressBar(R.id.status_progress, 10, 0, false); contentView.setTextViewText(R.id.status_text,currentFile); notification.contentView = contentView; // Toast.makeText(DownloadService.this, "Downloading...!",Toast.LENGTH_SHORT).show(); } @Override protected String doInBackground(String... aurl) { int count; try { //Downloading code goes here } catch (Exception e) {} return null; } protected void onProgressUpdate(String... progress) { Log.d("ANDRO_ASYNC",progress[0]); notification.contentView.setProgressBar(R.id.status_progress, 100, Integer.parseInt(progress[0]), false); // inform the progress bar of updates in progress notificationManager.notify(NOTIFICATION_ID, notification); } @Override protected void onPostExecute(String unused) { Toast.makeText(DownloadService.this, "Download complete!",Toast.LENGTH_SHORT).show(); } } 

The personal DownloadFileAsync class is located inside the DownloadService class, which extends Service. I can not show or update the notification bar.

I am currently getting an IllegalArgumentException in the line: notificationManager.notify (NOTIFICATION_ID, notification);

Thank you for your help in this!

EDIT: NOTIFICATION_ID is defined as follows: private int NOTIFICATION_ID = R.string.app_name

+4
source share
2 answers

It may be too late to answer this question, but now I am programming an application with similar code.

NOTIFICATION_ID must be of type int, because the first notify () parameter accepts an int, not a string, as in your code.

Read more about the notification and its various signatures below:

http://developer.android.com/reference/android/app/NotificationManager.html#notify(int,android.app.Notification )

0
source

I think you need to add your notification to the notification manager. I am not sure about that. Give it a try and let me know. Here is my code:

 notificationManager = (NotificationManager) getApplicationContext().getSystemService( getApplicationContext().NOTIFICATION_SERVICE); notificationManager.notify(10, notification); // simulate progress Thread download = new Thread() { @Override public void run() { for (int i = 1; i < 100; i++) { progress++; notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false); // inform the progress bar of updates in progress notificationManager.notify(id, notification); try { Thread.sleep(125); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; download.run(); 
0
source

All Articles