java 如何从我的应用程序向 Android 主屏幕添加小部件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16100926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a widget to the Android home screen from my app?
提问by Adam Arold
I'm writing an application which should be able to add widgets (just text boxes) to the home screen of the user's phone when the user instructs my app to do so. How can I do such a thing?
我正在编写一个应用程序,当用户指示我的应用程序这样做时,它应该能够将小部件(仅文本框)添加到用户手机的主屏幕。我怎么能做这样的事情?
I know that I can add an app widget but how about adding more?
我知道我可以添加一个应用小部件,但是添加更多小部件怎么样?
回答by stinepike
It is not possible from a app to place a widget in the home screen. Only the home screen can add app widgets to the home screen.
应用程序无法在主屏幕中放置小部件。只有主屏幕可以将应用程序小部件添加到主屏幕。
similar links link1, link2, link3
But you can offer user to pick widget from widgetpicker.
但是您可以让用户从widgetpicker 中选择widget。
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetID);
startActivityForResult(pickIntent, KEY_CODE);
回答by DalvikDroid
This was answered a long time ago, but in case anyone stumbles upon this question I thought I should provide an up-to-date answer.
这是很久以前的回答,但如果有人偶然发现这个问题,我想我应该提供一个最新的答案。
As of Android O (API 26), you can now pin widgets to the user's launcher from your app!
从 Android O (API 26) 开始,您现在可以将小部件从您的应用程序固定到用户的启动器!
Simply create the widget in your app's AndroidManifest file and use AppWidgetManagerto request that the widget be pinned to the launcher. Note that it is up to the launcher to support this feature, so you must call AppWidgetManager's isRequestPinAppWidgetSupported()method before requesting to pin it.
只需在应用程序的 AndroidManifest 文件中创建小部件并使用AppWidgetManager请求将小部件固定到启动器。请注意,是否支持此功能取决于启动器,因此您必须在请求固定它之前调用 AppWidgetManager 的isRequestPinAppWidgetSupported()方法。
Here's some documentation from Google that goes into more detail: https://developer.android.com/preview/features/pinning-shortcuts-widgets.html#widgets
以下是来自 Google 的一些更详细的文档:https: //developer.android.com/preview/features/pinning-shortcuts-widgets.html#widgets
Hope this helps!
希望这可以帮助!
Edit: It looks like the documentation pages have changed since I posted this answer. Here is a more helpful link that gives a code example of how to pin a widget to a launcher: https://developer.android.com/guide/topics/appwidgets/#Pinning
编辑:自我发布此答案以来,文档页面似乎已更改。这是一个更有用的链接,提供了如何将小部件固定到启动器的代码示例:https: //developer.android.com/guide/topics/appwidgets/#Pinning
回答by Juozas Kontvainis
AFAIK default launcher app does not support this. The reason is that user should place everything on the home screen himself. Allowing to place widgets from an application would open doors for apps to "spam" user's home with their "useful" widgets.
AFAIK 默认启动器应用程序不支持此功能。原因是用户应该自己将所有内容放在主屏幕上。允许放置来自应用程序的小部件将为应用程序打开大门,以使用其“有用的”小部件“向用户发送垃圾邮件”。