通知区域中的进度条未关闭且未启动第二个进度条

时间:2011-11-12 05:52:44

标签: android jprogressbar notification-area

在我的应用程序中我有4个按钮,当用户点击任何按钮时,它开始下载文件,进度条显示在通知区域中。下载和进度条工作正常,但我有以下两个问题

  1. 下载完成后,进度条未关闭,它仍保留在通知区域中
  2. 正如我上面说的,我有4个按钮,当点击第一个按钮时,下载开始,当点击其他三个按钮时,立即下载不会发生。我认为它可能在第一次下载完成后开始。但没有任何反应。单击所有按钮时如何显示所有进度条
  3. 以下是我的代码(这里我只添加了2个按钮)请帮助我

    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);
                }
            });
    

    接下来是我的 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();
        }
    

2 个答案:

答案 0 :(得分:1)

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

1-在上面这一行中,getActivity()中的第二个参数是requestCode,它应该是每个按钮的唯一编号。目前两个按钮都是0。

notificationManager.notify(42, notification);

2-在上面一行中,您传递的通知索引为42,对于这两个按钮应该是唯一的。目前,无论您创建多少个按钮,它都不会向您显示新的通知,因为您每个通过42个。它会不断更新当前版本。

您也可能需要再次查看您在onHandleIntent()中执行的代码。有更好的方法来做到这一点。

答案 1 :(得分:0)

您需要的是AsyncTaskListView。 你的下载可能发生在AsyncTask中。 例如,使用一个Array来指示正在运行的下载以及每个正在运行的下载的进度。 现在在你的AsyncTask中有一个publishProgress - 方法调用onProgressUpdate。 在onProgressUpdate中,您必须更新相应的进度变量并在ListView适配器上调用notifyDataSetChanged。 这将导致ListView重新加载所有数据。 最后在您的ListView - 适配器中,您获得了一个方法getView,您必须在其中为ListView的每一行创建视图。 在那里,您可以决定是否显示ProgressBar(下载正在运行)。

有关AsyncTask的更多信息,请访问:http://labs.makemachine.net/2010/05/android-asynctask-example/ 关于ListView的更多信息:Android : BaseAdapter how to?