我有这样的代码:
public class ImagesLoadedReciver extends BroadcastReceiver {
private static String[] mImagesUrls = new String[0];
private static final ImagesLoadedReciver INSTANCE = new ImagesLoadedReciver();
private static final List<View> mBackgrounds = new ArrayList<>();
@Override
public void onReceive(final Context context, final Intent intent) {
String action = intent.getAction();
if (BROADCAST_ACTION_IMAGES_LOADED.equals(action)) {
mImagesUrls = intent.getStringArrayExtra(BROADCAST_PARAM_IMAGES_NAMES);
for (View backgroundView : mBackgrounds){
setBackround(backgroundView,backgroundView.getContext());
}
}
}
public void registerBackground(View view){
mBackgrounds.add(view);
if(mImagesUrls.length!=0){
setBackround(view, view.getContext());
}
}
public void setBackround(final View view, Context context){
Picasso.with(context).load(mImagesUrls[0]).into(new Target() {
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
view.setBackground(new BitmapDrawable(view.getResources(), bitmap));
}
});
}
public static ImagesLoadedReciver getInstance() {
return INSTANCE;
}
}
我有服务,每隔15分钟从互联网上收集网址并发送BROADCAST_ACTION_IMAGES_LOADED。 Receiver接受它并使用Picasso将图像下载到mBackground中的每个View.To在mBackgrounds中添加View我使用方法registerBackground(),我在onResume()中的Activities中调用它。
并且存在问题:只有重新显示活动才会看到背景图片。它通常被注册并添加到receiver.mBackgrounds,但在onRecive()完成后,没有任何反应。
我使用私有静态最终ImagesLoadedReciver INSTANCE = new ImagesLoadedReciver(); 在我的代码的任何部分使用接收器的一个实例。
提前感谢大家的解答!