AppWidgetManager.getAppWidgetIds in activity returns an empty list

前端 未结 3 537
感动是毒
感动是毒 2021-02-06 08:34

I have an appwidget that I\'m trying to update from an activity.

To do that, I need the appwidget id.

I\'ve used AppWidgetManager.getAppWidgetIds bu

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 09:31

    To get the app widget ID.. use this below code

    onReceive() method use this below code

    int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
    AppWidgetManager.INVALID_APPWIDGET_ID);
    

    in onUpdate() you get the list of ids already as the parameter!

    If you want to update it from activity use this

     Intent intent = new Intent(this, [activityname].class);
        intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
        int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), [activityname].class));
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
        sendBroadcast(intent);
    

提交回复
热议问题