Android: enable / disable application widgets programmatically

Question Is there a way to enable some of the desktop widgets that I issue using my application programmatically? For example, having a "premium" widget and giving it access only after payment?


According to Android docs , you need to add the broadcast recipient to the manifest in order to tell the system that there is a widget that comes with the application

<receiver android:name="ExampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver> 

But theoretically, broadcast receivers can also be added programmatically, so that widgets can be registered later at runtime?

+5
source share
1 answer

You can have android:enabled="false" the <receiver> element for the application widget in the manifest, then use the PackageManager and setComponentEnabledSetting() to enable it at runtime when the user does something (e.g. paying).

However, it is possible that this will require a reboot before the home screen realizes that there is a new potential provider of application widgets.

+5
source

Source: https://habr.com/ru/post/1258288/


All Articles