【问题标题】:Notification Update onPostExecute AsyncTaskPostExecute AsyncTask 上的通知更新
【发布时间】:2018-10-05 10:52:03
【问题描述】:
<-- 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 通知不更新

有人知道我哪里错了吗?

【问题讨论】:

  • 这是在 android 文档中编写的用于更新通知的内容。Caution: Android applies a rate limit when updating a notification. If you post updates to a notification too frequently (many in less than one second), the system might drop some updates. 您在进度更新中多次更新通知。这可能是问题所在。
  • 任何避免此问题的解决方案@VivekMishra
  • 我觉得你可以从进度更新方法中删除通知
  • 那么如何在通知中显示下载过程? @VivekMishra

标签: java android notifications


【解决方案1】:

从您的代码看来,您对不同的通知使用相同的 id,因此onPreExecute() 中的第一个通知将显示,但onPostExecute() 中的第一个通知可能不会显示。

【讨论】:

  • 我正在使用相同的 id 先生,我想念有问题的问题对不起错过理解
  • 如果你想使用相同的id,你需要在再次通知之前调用cancel。检查这个stackoverflow.com/questions/29965263/…
  • 如果你取消,那么每次都会有新的通知。
  • 我想更新通知我不想取消它
  • 这不应该是build.setProgress(100, 0, false);吗?下载后要隐藏进度吗?
【解决方案2】:

感谢所有帮助我的人,我昨天解决了它

 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】:

    请尝试此解决方案

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

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多