Reputation: 5431
Is it possible to find out if the android widget is placed on lock screen or home screen? I'd imagine it's somewhere in the broadcast provider so I can find out how user is using it on 4.2+.
Upvotes: 3
Views: 1084
Reputation: 9375
Here are the relevant snippets from the documentation:
"You can detect whether your widget is on the keyguard (lockscreen) or home screen by calling getAppWidgetOptions()
to get the widget's options as a Bundle.
The returned bundle will include the key OPTION_APPWIDGET_HOST_CATEGORY
, whose value will be one of WIDGET_CATEGORY_HOME_SCREEN
or WIDGET_CATEGORY_KEYGUARD
. "
AppWidgetManager appWidgetManager;
int widgetId;
Bundle myOptions = appWidgetManager.getAppWidgetOptions (widgetId);
// Get the value of OPTION_APPWIDGET_HOST_CATEGORY
int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
// If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
"Once you know the widget's category, you can optionally load a different base layout, set different properties, and so on. For example:"
int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
Upvotes: 6