我试图从我的网络服务器加载图像并将其显示为Android widget
. 我也尝试每 60 分钟更新一次小部件,因为图像可以更改。图像应该也是clickable
如此。我让可点击部分与按钮完美配合,但是当我在布局中将其更改为 ImageView 时,点击部分停止工作。而且我根本无法加载图像。这是我的代码
@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);
}