ウィジェット内のテキストと画像を変更するためのボタンが 2 つあるウィジェットを作成したかったのですが、問題は、サービスから目的の位置を AppWidgetProvider クラスに送信できないことです。 appWidgetProvider に送信したエクストラ .. これが私のコードです ... onUpdate の前に onReceive を呼び出したい
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);
}