onPostExecute AsyncTask的通知更新

时间:2018-10-05 10:52:04

标签: java android notifications

<-- 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 Notification不更新

有人知道我要去哪里了吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

首先我是Cancle我的通知,然后再次显示

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

        }

答案 2 :(得分:0)

请尝试此解决方案

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



        }*/
}