2016-01-08 37 views
-4

我面临的问题在异步任务。我想从URl下载图像,并希望显示通知栏中的下载更新。OnPreExecute和OnPostExecute工作正常,但OnProgressUpdate不会调用。Android的AsyncTask的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); 


    } 

} 

}

+0

请阅读文档...这是当'onProgressUpdate'将被称为 – Selvin

+1

你不叫写作'publishProgress'随时随地 – Blackbelt

+0

尝试此链接http://www.androidhive.info/2012/04/ android-downloaded-file-by-showing-progress-bar/ – Joker

回答

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


} 
+0

仍然不能正常工作 – TechnologyLearner

+0

@rohitkhede尝试在RunOnUIThread中写入onProgressUpdate的代码。 –

相关问题