Android的updateAppWidget被调用但是小部件没有更新或者没有到达RemoteViewsFactory?
创始人
2024-10-07 10:01:37
0

在RemoteViewsService中,需要确保使用setRemoteAdapter方法将RemoteViewsFactory与适配器相关联,并确保在onDataSetChanged()方法中更新适配器数据。此外,还需要确保在RemoteViewsFactory中正确实现getViewAt()方法以及在RemoteViewsService中正确配置RemoteViewsFactoryIntent。

具体示例:

RemoteViewsService:

public class MyRemoteViewsService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new MyRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

public class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

    private List mList = new ArrayList<>();
    private Context mContext;
    
    public MyRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
    }
    
    @Override
    public void onDataSetChanged() {
        //更新适配器数据
        mList.clear();
        mList.add("Item 1");
        mList.add("Item 2");
        mList.add("Item 3");
    }
    
    @Override
    public RemoteViews getViewAt(int position) {
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.list_item);
        rv.setTextViewText(R.id.list_item_textview, mList.get(position));
        return rv;
    }
    
    //省略其他方法
    
}

RemoteViewsService中关联适配器及配置Intent:

public class MyWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.mywidget);
            
            //关联适配器
            Intent adapterIntent = new Intent(context, MyRemoteViewsService.class);
            rv.setRemoteAdapter(R.id.listview, adapterIntent);
            
            //配置Intent,确保点击列表项可以响应事件
            Intent clickIntent = new Intent(context, MyWidgetProvider.class);
            clickIntent.setAction(ACTION_CLICK);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);    
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
            rv.setPendingIntentTemplate(R.id.listview, pendingIntent);
            
            appWidgetManager.updateAppWidget(appWidgetId, rv);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
    
    //省略其他方法
}

相关内容

热门资讯

辅助透视"哈糖大菠萝... 辅助透视"哈糖大菠萝软件下载"详细辅助存在挂教程(私人局可以透视技巧)这是由厦门游乐互动科技有限公司...
透视规律"wepok... 透视规律"wepoker俱乐部辅助"详细辅助2025新版教程(破解辅助插件方法)是一款可以让一直输的...
透视app"wepo... 透视app"wepoker怎么下载游戏"详细辅助第三方教程(辅助器要钱玩软件);玩家必备必赢加哟《1...
透视挂"aapoke... 透视挂"aapoker辅助器怎么用"详细辅助扑克教程(免费脚本辅助)1、许多玩家不知道aapoker...
透视好友房"扑克之星... 透视好友房"扑克之星辅助"详细辅助第三方教程(有用软件);支持2-10人实时对战,虚拟庄家发牌,适配...
透视app"佛手大菠... 1、透视app"佛手大菠萝辅助"详细辅助揭秘攻略(有辅助软件)。2、佛手大菠萝辅助透视辅助简单,佛手...
透视脚本"wepok... 透视脚本"wepoker一直输的号能继续打吗"详细辅助玩家教程(有脚本技巧)是一款可以让一直输的玩家...
透视计算"wepok... 您好,wepoker代打辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视科技"拱趴大菠萝... 您好,拱趴大菠萝万能辅助器这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
透视计算"aapok... 透视计算"aapoker发牌逻辑"详细辅助黑科技教程(靠谱软件);大神普及一款德州ai内幕,确定了a...