开发者

Updating Progressbar in Notification bar using AsyncTask in a Service?

I'm trying to download multiple binary files using a Service which contains an AsynTask for doing background work. I'm able to run the Service successfully, I've registered it in the Manifest file. I'm able to download the files, however I would like to show a progress bar in the notification bar. I am creating the Notification bar inside the onPreExecute() method and setting the progressbar and notifying the 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",progres开发者_如何转开发s[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 DownloadFileAsync private class is inside the DownloadService class which extends Service. I'm unable to show or update the notification bar.

I'm currently getting an IllegalArgumentException on the line: notificationManager.notify(NOTIFICATION_ID, notification);

Would really appreciate your help on this one!

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


Probably late to answer this question, but I am currently programming an app that has similar code.

NOTIFICATION_ID has to be of the type int because the first parameter of notify() takes an int and not a string as in your code.

Here is more about notify and its different signatures:

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


I think you need to add your notification to notification manager.Iam not sure about it.U 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜