androidwidgetglance-appwidget

How to check if a widget cannot be installed, because system is not supporting them?


To check if a widget is placed on screen I use this function

override suspend fun checkWidgetsOnScreen(): Boolean =
        withContext(defaultDispatcher) {
            val glanceId = GlanceAppWidgetManager(applicationContext).getGlanceIds(SubscriptionsWidget::class.java).lastOrNull()
            return@withContext (glanceId != null)
        }

But there are cases when system is not supported widgets at all, for example on Android Go. I do not want to advise user to place widget on screen when the system is not supporting it.

How should I check this?


Solution

  • The android.software.app_widgets feature indicates if the device supports app widgets, such as those created by Glance. You can use hasSystemFeature() to see if the device supports that feature:

    context.packageManager.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)
    

    That snippet returns true if the device supports app widgets, false otherwise.

    If your app is dominated by the app widget, such that you only want to have your app installed on devices that support app widgets, you could also add this to your manifest:

    <uses-feature android:name="android.software.app_widgets" android:required="true" />