【问题标题】:update android widget (using async task) with an image from the internet使用来自互联网的图像更新 android 小部件(使用异步任务)
【发布时间】:2013-06-29 21:08:55
【问题描述】:

我有一个简单的 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?
    } 
}

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

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

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

谢谢!

【问题讨论】:

  • 为 DownloadBitmap 创建一个构造函数并在调用 .execute 之前构造该类

标签: android android-asynctask widget


【解决方案1】:

一种解决方案是将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);
    }
}

【讨论】:

  • 非常感谢!!!有用!你是人间的神。我调整了你的想法,我的最终答案可以在下面看到。
  • 我认为这也可以解决我的问题,但我不知道如何实现它:stackoverflow.com/questions/20792944/…
【解决方案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);
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多