Android AsyncTask的onProgressUpdate未被调用

时间:2016-01-08 09:45:20

标签: android

我在Async Task中遇到问题。我想从URl下载图片,并希望在通知栏中显示下载更新.OnPreExecute和OnPostExecute工作正常,但OnProgressUpdate未调用。

 public class DownloadImageActivity extends AppCompatActivity {
int id = 1;
ImageView image;
private NotificationManager mNotifyManager;
private Builder mBuilder;
private String URL = "http://www.tivix.com/uploads/blog_pics/Android-logo.png";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_progress);
    image = (ImageView) findViewById(R.id.imageView);
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mBuilder = new NotificationCompat.Builder(DownloadImageActivity.this);
            mBuilder.setContentText("Download in progress")
                    .setSmallIcon(R.drawable.ic_action_download);

            new DownloadImage().execute(URL);
        }
    });
}

private class DownloadImage extends AsyncTask<String, Integer, Bitmap> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Displays the progress bar for the first time.
        mBuilder.setProgress(100, 0, false);

        mNotifyManager.notify(id, mBuilder.build());

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // Update progress

        CharSequence title = "Downloading: " + values[0] % 100 + "%";
        mBuilder.setContentTitle(title);
        mBuilder.setProgress(100, values[0] % 100, false);
        mNotifyManager.notify(id, mBuilder.build());
        super.onProgressUpdate(values);
    }

    @Override
    protected Bitmap doInBackground(String... URL) {


        String imageURL = URL[0];

        Bitmap bitmap = null;
        try {

            InputStream input = new java.net.URL(imageURL).openStream();
            bitmap = BitmapFactory.decodeStream(input);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        mBuilder.setContentText("Download complete");
        // Removes the progress bar
        mBuilder.setProgress(0, 0, false);
        mNotifyManager.notify(id, mBuilder.build());
        image.setImageBitmap(result);


    }

}

}

1 个答案:

答案 0 :(得分:0)

使用super.onProgressUpdate(值);在方法的开头。

 @Override
protected void onProgressUpdate(Integer... values) {
    // Update progress
    super.onProgressUpdate(values);
    CharSequence title = "Downloading: " + values[0] % 100 + "%";
    mBuilder.setContentTitle(title);
    mBuilder.setProgress(100, values[0] % 100, false);

    runOnUiThread(new Runnable() {
    @Override
    public void run() {
         mNotifyManager.notify(id, mBuilder.build());
    }
});


}