How to bring up a widget on home screen programatically

旧街凉风 提交于 2019-12-04 13:34:25

问题


Programmatically I'm trying to bring a widget on the home screen but I am facing problem while passing the widget ID through an intent & calling startActivity(pickIntent). The widget list is coming up but widget which I have developed is not getting selected.

final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(component);
Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
}

I am getting extras= null here. So, I tried allocating widget ID as WidgetId = mAppwidgetHost.allocateAppWidgetId();

Sent through the intent:

Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
        pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, WidgetId);
        startActivity(pickIntent);

but still my widget is not getting selected. Can anyone please let me know how do I bring up a widget on home screen, without any user interaction. Thanks in advance.


回答1:


You can't do this: the standard widget picker does not allow you to specify a specific widget.



来源:https://stackoverflow.com/questions/6659776/how-to-bring-up-a-widget-on-home-screen-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!