使用来自互联网的图像更新android小部件(使用异步任务)

时间:2013-06-29 12:29:07

标签: android android-asynctask widget

我有一个简单的Android小部件,我想用互联网上的图像更新。我可以在小部件上显示静态图像没问题。我被告知你需要使用异步任务,我对这些没有多少经验。

这是我的小部件:

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        super.onUpdate(context, appWidgetManager, appWidgetIds);

        for (int i = 0; i < appWidgetIds.length; i++){
            int appWidgetId = appWidgetIds[i];      

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);

                //Setup a static image, this works fine.
            views.setImageViewResource(R.id.imageView1, R.drawable.wordpress_icon);             

            new DownloadBitmap().execute("MyTestString");       

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }

然后我有一个执行下载的异步任务类。它看起来像这样:

public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> {

    /** The url from where to download the image. */
    private String url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"; 

    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            InputStream in = new java.net.URL(url).openStream();
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            return bitmap;              
            //NOTE:  it is not thread-safe to set the ImageView from inside this method.  It must be done in onPostExecute()
        } catch (Exception e) {
            Log.e("ImageDownload", "Download failed: " + e.getMessage());
        }
        return null;
    }


    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

           //Here is where I should set the image to the imageview, but how?
    } 
}

我认为我的代码已经成功从互联网上下载了图像。

我很困惑的是,如何将这个图像从我的Async任务类中获取到特定小部件的“ImageView”中。要更新图像,您需要访问3个不同的对象:Context,AppWidgetManager和AppWidgetId ....但是如何在此语句中传递所有这些对象:???

new DownloadBitmap().execute("MyTestString");

谢谢!

2 个答案:

答案 0 :(得分:9)

一种解决方案是将RemoteViews作为参数传递给DownloadBitmap构造函数,并在onPostExecute()中设置图像:

在onUpdate()中:

new DownloadBitmap(views).execute("MyTestString");

并在DownloadBitmap中:

//....
public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> {
    private RemoteViews views;

    public DownloadBitmap(RemoteViews views){
        this.views = views;
    }

    //.....
    public void onPostExecute(Bitmap bitmap){
        views.setImageViewBitmap(R.id.imageView1, bitmap);
    }
}

答案 1 :(得分:2)

请参阅上面的Andy Res的解决方案。我稍微调整它以传递更多参数......但它有效!!!!

我称之为:

new DownloadBitmap(views, appWidgetId, appWidgetManager).execute("MyTestString");

然后,我的任务就像这样开始:

public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> {

    /** The url from where to download the image. */
    private String url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"; 

    private RemoteViews views;
    private int WidgetID;
    private AppWidgetManager WidgetManager;

    public DownloadBitmap(RemoteViews views, int appWidgetID, AppWidgetManager appWidgetManager){
        this.views = views;
        this.WidgetID = appWidgetID;
        this.WidgetManager = appWidgetManager;        
    }

@Override
protected Bitmap doInBackground(String... params) {
    try {
        InputStream in = new java.net.URL(url).openStream();
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        Log.v("ImageDownload", "download succeeded");                       
        Log.v("ImageDownload", "Param 0 is: " + params[0]); 
        return bitmap;              
        //NOTE:  it is not thread-safe to set the ImageView from inside this method.  It must be done in onPostExecute()
    } catch (Exception e) {
        Log.e("ImageDownload", "Download failed: " + e.getMessage());
    }
    return null;
}


@Override
protected void onPostExecute(Bitmap bitmap) {
    if (isCancelled()) {
        bitmap = null;
    }

    views.setImageViewBitmap(R.id.imageView1, bitmap);
    WidgetManager.updateAppWidget(WidgetID, views);
}