Search code examples
androidjprogressbarnotification-area

progress bar in Notification area is not closing and not starting the second progress bar


in my app i have 4 buttons and when the user clicks any of the button it starts downloading a file and the progress bar gets shown in the notification area. The downloading and progress bar is working fine, but i have the following two problems

  1. When the download completes the progress bar is not getting closed, it remains in the notification area
  2. As i said above i have 4 buttons and when the first button is clicked download gets started and when the other three buttons are clicked immediately download is not taking place. I thought it may start after first download completes. But nothing happens. How to show all the progress bar when all buttons clicked

Following is my code(here i have added only 2 buttons) pls help me

b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                i =1;
                Intent intent = new Intent(NotificationProgressTestActivity.this, UploadService.class);
                startService(intent);
            }
        });

        b2 = (Button)findViewById(R.id.button2);
        b2.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                i = 2;
                Intent intent = new Intent(NotificationProgressTestActivity.this, UploadService.class);
                startService(intent);
            }
        });

Next following is my Uplaod Service.class

public class UploadService extends IntentService
{   
    private NotificationManager notificationManager;
    private Notification notification;
    private int progress = 10;
    private static String fileName = "folder/";
    private static URL url;
    public UploadService(String name) 
    {
        super(name);
    }
    public UploadService()
    {
        super("UploadService");
    }
    @Override
    protected void onHandleIntent(Intent intent) 
    {
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new Notification(R.drawable.icon,"Uploading file", System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.upload_progress_bar);
        notification.contentIntent = contentIntent;
        notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
        notificationManager.notify(42, notification);
        notificationManager.notify(42, notification);
        Thread download = new Thread() 
        {
            @Override
            public void run() 
            {
                Log.e("download", "start");
                try
                {
                    for (int i = 1; i < 100; i++) 
                    {    
                        progress++;
                        notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
                        if(i==1)
                        {  
                            if(NotificationProgressTestActivity.i ==1 )
                            {
                                url = new URL("http://xxxxxxxxxxxxxxxx.mp4");
                            }
                            else if(NotificationProgressTestActivity.i == 2)
                            {
                                url = new URL("http://xxxxxxxxxxxxxxxx.mp4");
                            }
                            HttpURLConnection c = (HttpURLConnection) url.openConnection();
                            c.setRequestMethod("GET");
                            c.setDoOutput(true);
                            c.connect();

                            String PATH = Environment.getExternalStorageDirectory()+ "/";
                            Log.e("PATH:", PATH);
                            File file = new File(PATH);
                            if (!file.exists()) 
                            {
                                file.mkdir();
                                Log.e("destination", "created");
                            } 
                            else 
                            {
                                Log.e("destination", "exist");
                            }
                            File outputFile = new File(file, fileName);
                            FileOutputStream fos = new FileOutputStream(outputFile);

                            InputStream is = c.getInputStream();

                            byte[] buffer = new byte[10171188];
                            int len1 = 0;
                            while ((len1 = is.read(buffer)) != -1) 
                            {
                                fos.write(buffer, 0, len1);
                            }
                            fos.close();
                            is.close();
                            //  -----------------------
                            if (!outputFile.exists()) 
                            {
                                Log.e(outputFile.toString(), "not created");
                            }
                            else 
                            {
                                Log.e(outputFile.toString(), "created");
                                Log.e(outputFile.toString(), "" + outputFile.length());
                            }
                            Log.e("download", "end");
                        }
                        notificationManager.notify(42, notification);
                        try 
                        {
                            Thread.sleep(1017);
                        }
                        catch (InterruptedException e) 
                        {
                            e.printStackTrace();
                        }
                 }
            }
            catch (IOException e) 
            {
                        Log.e("log_tag", "Error: " + e);
            }
            Log.e("log_tag", "Check: ");

                // remove the notification (we're done)
            notificationManager.cancel(42);
        }
     };
        download.run();
    }

Solution

  • PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    

    1- in this above line, the 2nd parameter in getActivity() is requestCode, it should be a unique number for each button. currently it is 0 for both buttons.

    notificationManager.notify(42, notification);
    

    2- in the above line, the notification index you are passing as 42, it should be unique for both buttons. currently no matter how many buttons you create it will never show you a new notification because you are passing 42 for each. it will keep updating the current one.

    also you might need to look again the code you are doing in onHandleIntent(). there are better ways to do this.