简体   繁体   English

如何在onReceive中获取给定appWidgetId的removeView

[英]How to obtain removeView for a given appWidgetId in onReceive

Recently, I had built up my widget UI, and install click event handler in onUpdate . 最近,我建立了我的窗口小部件UI,并在onUpdate安装了click事件处理程序。

I was wondering, what is the proper way for me to access remoteView , for a given appWidgetId , during onReceive ? 我想知道,在onReceive期间,对于给定的appWidgetId ,我访问remoteView的正确方法是什么? Is storing all removeView s in a local member map like 正在将所有removeView s存储在本地成员映射中,例如

Map<appWidgetId, remoteView>

a good idea? 一个好主意? Or, is there any better way? 还是有更好的方法?


import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MyAppWidgetProvider extends AppWidgetProvider {    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        for (int appWidgetId : appWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout_inverse_holo_light);

            // Register an onClickListener
            Intent refreshIntent = new Intent(context, JStockAppWidgetProvider.class);
            refreshIntent.setAction(REFRESH_ACTION);
            refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);            
            remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager mgr = AppWidgetManager.getInstance(context);
        if (intent.getAction().equals(REFRESH_ACTION)) {
            int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

            // How can I access my remoteView?
            // During onUpdate, is saving all the remoteViews in local member
            // Map<appWidgetId, remoteView> a good idea?
        }
        super.onReceive(context, intent);
    }

    private static final String REFRESH_ACTION = "org.yccheok.jstock.gui.widget.JStockAppWidgetProvider.REFRESH_ACTION";  
}

If you want to refresh the widget, just create a new RemoteView, and then call 如果要刷新小部件,只需创建一个新的RemoteView,然后调用

appWidgetManager.updateAppWidget

Or if your widget is backed by a CursorAdaptor of some sort, you should implement 或者,如果您的小部件由某种CursorAdaptor支持,则应实现

RemoteViewsService.RemoteViewsFactory

Any changes to the adaptor will then automatically updates the widget. 对适配器的任何更改将自动更新小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM