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
source share