这是一个简单的小部件,它显示了从外部存储器获取的一组位图。它基本上可以工作,但是让monit认为App在其主线程上做了太多工作,所以我把东西放在AsyncTask中,但是widget仍然是空的,没有加载位图。这是有效的代码:
public class PhotosLoader /*extends AsyncTask<Void, Void ,ArrayList<Bitmap>> */ {
private Context mContext;
private RemoteViews views;
private ArrayList<Bitmap> images;
public PhotosLoader (Context context, RemoteViews views){
mContext = context;
this.views = views;
}
public ArrayList<Bitmap> LoadPhotos() {
PhotosReader PR = new PhotosReader();
images = PR.getPhotosArray(mContext);
PhotosModifier PM = new PhotosModifier();
ArrayList<Bitmap> bitmaps = PM.getResizedPhotosBitmap(images);
return bitmaps;
}
public void inflatePhotos(ArrayList<Bitmap> bitmaps){
views.setImageViewBitmap(R.id.imageView, bitmaps.get(0));
views.setImageViewBitmap(R.id.imageView2, bitmaps.get(1));
views.setImageViewBitmap(R.id.imageView3, bitmaps.get(2));
views.setImageViewBitmap(R.id.imageView4, bitmaps.get(3));
views.setImageViewBitmap(R.id.imageView5, bitmaps.get(4));
}
}
onUpdate方法:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
PhotosLoader PL = new PhotosLoader(context, views);
ArrayList<Bitmap> bitmaps = PL.LoadPhotos();
PL.inflatePhotos(bitmaps);
}
}
并且有代码没有:
public class PhotosLoader extends AsyncTask<Void, Void ,ArrayList<Bitmap>> {
private Context mContext;
private RemoteViews views;
private ArrayList<Bitmap> images;
public PhotosLoader (Context context, RemoteViews views){
mContext = context;
this.views = views;
}
@Override
protected ArrayList<Bitmap> doInBackground(Void... voids) {
PhotosReader PR = new PhotosReader();
images = PR.getPhotosArray(mContext);
PhotosModifier PM = new PhotosModifier();
ArrayList<Bitmap> bitmaps = PM.getResizedPhotosBitmap(images);
return bitmaps;
}
@Override
protected void onPostExecute(ArrayList<Bitmap> bitmaps) {
super.onPostExecute(bitmaps);
views.setImageViewBitmap(R.id.imageView, bitmaps.get(0));
views.setImageViewBitmap(R.id.imageView2, bitmaps.get(1));
views.setImageViewBitmap(R.id.imageView3, bitmaps.get(2));
views.setImageViewBitmap(R.id.imageView4, bitmaps.get(3));
views.setImageViewBitmap(R.id.imageView5, bitmaps.get(4));
}
}
onUpdate:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
new PhotosLoader(context, views).execute();
Log.d("Main","onUpdate");
}
}
那些只是项目中的代码更改,我第一次执行AsyncTask,所以Idk我做错了什么,看起来很简单
答案 0 :(得分:1)
我认为问题在于您在之后尝试更新RemoteView对象,并从onUpdate
方法返回它。您应该将已经构建的内容返回并填入所需的数据。
也许尝试采用与传递位图不同的方法,并将Uri
设置为ImageView
上的图片?
编辑:实际上,还有一个问题可能是您实际上没有在任何地方更新窗口小部件:D您创建RemoteView然后删除它。你打电话给appWidgetManager.updateAppWidget
吗?