0

我试图从我的网络服务器加载图像并将其显示为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);
}
4

2 回答 2

0

您可以将图像设置为可点击部分的背景。

于 2012-07-30T04:15:46.130 回答
0

我设法解决了这个问题。我错误地设置了图像。

替换:

remoteViews.setBitmap(R.id.button, "setButton", buttonimg);

和:

remoteViews.setImageViewBitmap(R.id.button, buttonimg);

这完美无瑕!

于 2012-08-19T15:01:18.217 回答