1

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();
    }
Siva K
  • 4,968
  • 14
  • 82
  • 161
  • i am really sorry, i thought of typing Problems and i misspelled s to be a. Just now i noticed, i will change my title – Siva K Nov 12 '11 at 06:14
  • 1
    Why do you have a loop if you're only actually going to do any real work in one of the iterations? This isn't going to give *meaningful* progress. – Jon Skeet Nov 14 '11 at 07:18

2 Answers2

1
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.

g.revolution
  • 11,962
  • 23
  • 81
  • 107
  • thanx a lot... your right and i got progress bar for each button. But still i have a problem...Only the first progress bar gets completed and not the others. And the first progress bar is not also getting closed when it gets completed... – Siva K Nov 21 '11 at 04:55
  • i am sorry the progress bar gets cancelled after completion but there is no progress change in other progress bars after a certain point for example to be 25%.... – Siva K Nov 21 '11 at 04:58
0

What you need is an AsyncTask and a ListView. Your downloadmagic happens in AsyncTask. Make for example an Array that indicates which download is running and the progress of each running download. Now in your AsyncTask there is a publishProgress-Method which calls onProgressUpdate. In onProgressUpdate you have to update the respective progress-variable and call notifyDataSetChanged on your ListView Adapter. This will cause the ListView to reload all Data. Finally in your ListView-Adapter you got a Method getView, where you have to create the View for each row of your ListView. There you can decide to show the ProgressBar (download running) or not.

More Information about AsyncTask can be found here: http://labs.makemachine.net/2010/05/android-asynctask-example/ And more about the ListView here: Android : BaseAdapter how to?

Community
  • 1
  • 1
Thommy
  • 5,070
  • 2
  • 28
  • 51
  • 1
    He's using a service that starts a worker thread, he doesn't need an AsyncTask. And if he had been using an AsyncTask, he would have received recommendations to swith to a service. – zmbq Nov 19 '11 at 23:30
  • agree, service is better than asynctask (think of all the trouble when rotating the screen/leaving the app for a few seconds!). Anyway, I believe a ''Loader'' *could* be a better option! (and it's available on the compatibility package) http://developer.android.com/guide/topics/fundamentals/loaders.html – Pedro Loureiro Nov 20 '11 at 11:02