是否有可能找出是否在Android窗口小部件置于锁屏或主屏幕上​​? [英] Is it possible to find out if the android widget is placed on lock screen or home screen?

查看:160
本文介绍了是否有可能找出是否在Android窗口小部件置于锁屏或主屏幕上​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能找出是否Android小工具置于锁屏或主屏幕上​​?
我想像它是介于广播​​供应商,所以我可以找出用户是如何使用它的4.2 +

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+.

推荐答案

下面是从<一个相关的片段href=\"http://developer.android.com/guide/topics/appwidgets/index.html#lockscreen\">documentation:

你可以通过调用 getAppWidgetOptions()来得到widget的选项作为包检测窗口小部件是否在键盘锁(锁屏)或主屏幕。

"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.

返回的包将包括关键 OPTION_APPWIDGET_HOST_CATEGORY ,其值将是 WIDGET_CATEGORY_HOME_SCREEN 中的一个或 WIDGET_CATEGORY_KEYGUARD

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;

这篇关于是否有可能找出是否在Android窗口小部件置于锁屏或主屏幕上​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆