我想制作一个小部件,它有两个 to 按钮来更改小部件内的文本和图像我的问题是我无法在 onRecieve 之前将所需位置从服务发送到 AppWidgetProvider 类调用 onUpdate 所以我无法得到我发送给 appWidgetProvider 的额外内容 .. 这是我的代码 ...我想让 onReceive 在 onUpdate 之前被调用
public class Youm7widget extends AppWidgetProvider {
private int position = 0;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
Log.d("on recieve", "here");
position = intent.getIntExtra("position", 0);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
Log.d("on update", "here");
ComponentName thisWidget = new ComponentName(context, Youm7widget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(),
WidgetConfiguration.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
intent.putExtra("position", position);
// Update the widgets via the service
context.startService(intent);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "Widget Deleted", Toast.LENGTH_SHORT).show();
}
}
这就是我用来更新和更改小部件上数据的服务
public class WidgetConfiguration extends Service {
private AppManager appManager;
private int[] allWidgetIds = null;
AppWidgetManager appWidgetManager;
private List<News> newsWidget;
private ImageLoader imageLoader = ImageLoader.getInstance();
private static DisplayImageOptions optionsWithFakeDisplayer;
private int position ;
static {
optionsWithFakeDisplayer = new DisplayImageOptions.Builder().displayer(
new FakeBitmapDisplayer()).build();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
appManager = AppManager.getInstance();
appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
position = intent.getIntExtra("position" , 4);
Toast.makeText(getApplicationContext(), String.valueOf(position),
Toast.LENGTH_LONG).show();
// get ids
if (allWidgetIds != null) {
} else {
allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
}
try {
newsWidget = loadXml(
"http://mobrss.youm7.com/rss/service.svc/SelectForSpecifiedSection/SecID/12/page/1",
true, "0");
for (final int widgetId : allWidgetIds) {
final RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),
R.layout.widget_layout);
remoteViews.setViewVisibility(R.id.W_loading_bar, View.VISIBLE);
remoteViews.setViewVisibility(R.id.Wrefresh_button, View.GONE);
remoteViews.setTextViewText(R.id.Winner_title, newsWidget
.get(0).getTitle().toString());
imageLoader.getInstance().loadImage(
newsWidget.get(position).getMainImageLink(), null,
optionsWithFakeDisplayer,
new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
// TODO Auto-generated method stub
super.onLoadingComplete(imageUri, view,
loadedImage);
remoteViews.setImageViewBitmap(
R.id.Winner_image, loadedImage);
appWidgetManager.updateAppWidget(widgetId,
remoteViews);
}
});
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),
Youm7widget.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
clickIntent.putExtra("position", 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.Wrefresh_button,
pendingIntent);
Intent in = new Intent(getApplicationContext(),
DetailsActivity.class);
in.putExtra("ID", newsWidget.get(position).getID());
PendingIntent pi = PendingIntent.getActivity(
getApplicationContext(), 0, in, 0);
remoteViews.setOnClickPendingIntent(R.id.Winner_image, pi);
// Build the intent to call the service
Intent fw = new Intent(this.getApplicationContext(),
Youm7widget.class);
fw.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
fw.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
fw.putExtra("position", 2);
PendingIntent fwpi = PendingIntent.getBroadcast(
getApplicationContext(), 0, fw,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.Wprevitem,
fwpi);
remoteViews.setViewVisibility(R.id.W_loading_bar, View.GONE);
remoteViews.setViewVisibility(R.id.Wrefresh_button, View.VISIBLE);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
} catch (IOException e) {
Log.d("IO exceptions", "ture");
Toast.makeText(getApplicationContext(), "IO exceptions",
Toast.LENGTH_LONG).show();
} catch (XmlPullParserException xm) {
Log.d("XML exceptions", "true");
Toast.makeText(getApplicationContext(), "XML exceptions",
Toast.LENGTH_LONG).show();
}
super.onStart(intent, startId);
}