How to bring up a widget on home screen programati

2019-03-22 01:03发布

问题:

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.