代码之家  ›  专栏  ›  技术社区  ›  Ashvin solanki

通知更新onPostExecute AsyncTask

  •  -1
  • Ashvin solanki  · 技术社区  · 6 年前
    <-- Android/Java --->
    
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder build;
    final int id = 101;
    
    private class Download extends AsyncTask<Void, Integer, Integer> {
    
          @Override
          protected void onPreExecute() {
             super.onPreExecute();
    
             // Displays the progress bar for the first time.
             build.setProgress(100, 0, false);
             mNotifyManager.notify(id, build.build());
          }
    
          @Override
          protected void onProgressUpdate(Integer... values) {
             // Update progress
             build.setProgress(100, values[0], false);
             mNotifyManager.notify(id, build.build());
             super.onProgressUpdate(values);
          }
    
          @Override
          protected Integer doInBackground(Void... params) {
                //.........
    
                        FileOutputStream f = new FileOutputStream(new File(rootFile,
                                arg0[1]));
    
                        InputStream in = c.getInputStream();
                        byte[] buffer = new byte[1024];
                        int len1 = 0;
                        float conLen = c.getContentLength();
                        float total = 0;
                        while ((len1 = in.read(buffer)) > 0) {
                            f.write(buffer, 0, len1);
                            total += len1;
                            publishProgress((int) (total * 100 / conLen));
                        }
                        f.close();
    
             return result;
          }
    
          @Override
          protected void onPostExecute(Integer result) {
             super.onPostExecute(result);
             Log.e(TAG,"in PostExecute");
             build.setContentText("Download complete");
             // Removes the progress bar
             build.setProgress(0, 0, false);
             mNotifyManager.notify(id, build.build());
          }
       }
    
    }
    

    每个人都认为工作很好,但当我在 postExecute通知未更新

    有人知道我哪里出错了吗?

    2 回复  |  直到 6 年前
        1
  •  1
  •   karan    6 年前

    从您的代码看,您似乎使用相同的ID来处理不同的通知,因此 onPreExecute() 只会出现在 onPostExecute() 可能不会出现。

        2
  •  0
  •   Ashvin solanki    6 年前

     protected void onPostExecute(Integer result) {
         super.onPostExecute(result);
         DownloadDone();
      }
    

    首先我取消我的通知,然后再显示

    public void DonwloadDone()
            {
                mNotifyManager.cancel(id);
                mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                build = new NotificationCompat.Builder(getBaseContext());
                build.setContentTitle("Download")
                        .setContentText("Complete")
                        .setSmallIcon(R.drawable.ic_launcher_foreground);
                Notification notification = build.build();
                mNotifyManager.notify(id, notification);
                sendMyBroadCast("Complete",fx.getPath());
    
            }
    
        3
  •  0
  •   S_i_l_e_n_t C_o_d_e_r Spiritofthecore    6 年前

    请尝试此解决方案

     private class Download extends AsyncTask<Void, Integer, Integer> {
        //        int NOTIFICATION_ID = 1;
        int PROGRESS_MAX = 100;
        int PROGRESS_CURRENT = 0;
        int NOTIFICATION_ID = 1;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            // Displays the progress bar for the first time.
            build.setProgress(100, 0, false);
            mNotifyManager.notify(NOTIFICATION_ID, build.build());
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            // Update progress
    //            updateExportProgress(100);
    //            build.setProgress(100, tempProgress, false);
            build.setContentText("Downloading... " + tempProgress * 100 / PROGRESS_MAX + 
    "%")
                    .setProgress(PROGRESS_MAX, tempProgress, false);
            mNotifyManager.notify(NOTIFICATION_ID, build.build());
            super.onProgressUpdate(values);
        }
    
        @Override
        protected Integer doInBackground(Void... params) {
            int i;
            for (i = 0; i <= tempProgress; i += 5) {
                // Sets the progress indicator completion percentage
                publishProgress(Math.min(i, 100));
                try {
                    // Sleep for 5 seconds
                    Thread.sleep(2 * 1000);
                } catch (InterruptedException e) {
                    Log.d("Failure", "sleeping failure");
                }
            }
            return null;
        }
    
    
        @Override
        protected void onPostExecute(Integer filePath) {
            super.onPostExecute(filePath);
            build.setContentText("Download complete")
                    .setProgress(0, 0, false);
    
    
    
            }*/
    }