How to add a widget to the Android home screen from my app?

后端 未结 3 1072
夕颜
夕颜 2020-12-04 20:20

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

相关标签:
3条回答
  • 2020-12-04 20:55

    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!

    Simply create the widget in your app's AndroidManifest file and use AppWidgetManager to 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.

    Here's some documentation from Google that goes into more detail: 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

    0 讨论(0)
  • 2020-12-04 20:56

    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.

        Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
        pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetID);
        startActivityForResult(pickIntent, KEY_CODE);
    
    0 讨论(0)
  • 2020-12-04 21:01

    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.

    0 讨论(0)
提交回复
热议问题