OnUpdate 在 Android Widget 中的 onReceive 之前调用

Posted

技术标签:

【中文标题】OnUpdate 在 Android Widget 中的 onReceive 之前调用【英文标题】:OnUpdate called before onReceive in Android Widget 【发布时间】:2013-08-06 22:56:32 【问题描述】:

我想制作一个小部件,它有两个 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);

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

以上是关于OnUpdate 在 Android Widget 中的 onReceive 之前调用的主要内容,如果未能解决你的问题,请参考以下文章

android 创建桌面小部件widget

用于绑定集合的 Android Widget 服务类未被提供者调用

android BroadcastReceiver的子类静态注册action失效

ListView 未在 App Widget 中显示数据

onUpdate 未在小部件中调用,即使我在 onreceive 中看到 android.appwidget.action.APPWIDGET_UPDATE 意图

小部件onUpdate调用一次