Im trying to load an image from my webserver and display it as an Android widget. Im trying to have the widget update every 60 minutes as well since the image can change. The image should be clickable as well. I had the clickable part working perfectly with a button, but when I change it to an ImageView instead in my layout, the click portion ceases to work. And I cannot get the image to load at all. Here is my code
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent i = new Intent(context, LoadPage.class);
i.setAction(ACTION_WIDGET_LOAD);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0);
remoteViews.setOnClickPendingIntent(R.id.button, pi);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyButton(context, appWidgetManager), 1, 5000);
}
private class MyButton extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
public MyButton(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, ButtonWidget.class);
}
@Override
public void run() {
Bitmap buttonimg = null;
try {
String name = "http://badams.ca/otwstats/get_stats.php?user=badams";
URL url_value = new URL(name);
buttonimg = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
remoteViews.setBitmap(R.id.button, "setButton", buttonimg);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
I managed to fix this. I was setting the image incorrectly.
Replaced:
with:
and that worked flawlessly!