0

我正在构建一个主屏幕小部件,我的目标是在这个小部件中的 ImageView 上设置一个图像,并setImageViewResource()用于此目的。它工作得很好 -意味着目标图像在执行后将R.id.user_button其图像资源更改R.drawable.activity_notificationonStart()为- 如果我这样使用它:

public class UpdateWidgetService extends Service {
    public void onStart(Intent intent, int startId) {
        ... //Some code which is not really important
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(),R.layout.widget_layout);
        remoteViews.setImageViewResource(R.id.user_button, R.drawable.activity_notification);
    }
}

问题是我必须放入setImageViewResource()一个外部函数,当尝试构建类似下面的代码时它拒绝工作 -意味着目标图像的图像资源在执行R.id.user_button后保持不变onStart()

public class UpdateWidgetService extends Service {
    public void onStart(Intent intent, int startId) {
        ... //Some code which is not really important
        changeImagePicture();
    }
    public void changeImagePicture() {
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(),R.layout.widget_layout);
        remoteViews.setImageViewResource(R.id.user_button, R.drawable.activity_notification);
    }
}

我想我是个菜鸟,在这里我想念一些非常简单且非常重要的东西。你能指出我的错误,这样我才能让我的代码最终工作;)谢谢 beaucoup!

4

1 回答 1

5

您需要使用 AppWidgetManager 类的 updateAppWidget 方法:

AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);

祝你好运....

于 2012-09-17T07:49:22.737 回答