I have a simple widget for Android that I want to update using an image from the Internet. I can not show static images in widgets. I was told that for this you need to use the async task, and I do not have much experience with them.
Here is my widget:
@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);
Then I have an async task class that does the loading. It looks like this:
public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> { 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;
I think my code successfully downloaded the image from the Internet.
What I'm confused is how do I get this image in the “ImageView” of a particular widget from my Async task class. To update the image, you need access to 3 different objects: Context, AppWidgetManager and AppWidgetId .... But how to transfer all these objects inside this expression: ???
new DownloadBitmap().execute("MyTestString");
Thanks!
source share