OnUpdate在Android Widget中的onReceive之前调用

时间:2013-08-06 04:07:15

标签: android android-widget broadcastreceiver appwidgetprovider

我想创建一个小部件,其中有两个按钮来更改文本和小部件内的图像我的问题是我无法在onRecieve之前将所需的位置从服务发送到AppWidgetProvider类调用onUpdate,以便我无法获得我发送给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);
}

1 个答案:

答案 0 :(得分:5)

只需在自定义代码之后移动super.onReceive(),如下所示:

@Override
public void onReceive(Context context, Intent intent) {
    position = intent.getIntExtra("position", 0);
    super.onReceive(context, intent);
}

super.onReceive()将您的意图行动发送给他的处理方法。对于更新操作,它会立即调用onUpdate。所以你应该在那之前做任何你想做的事。