Skip to content

Instantly share code, notes, and snippets.

@waliahimanshu
Created November 18, 2020 10:28
Show Gist options
  • Save waliahimanshu/a90379e0673d36de7097349b4495eddd to your computer and use it in GitHub Desktop.
Save waliahimanshu/a90379e0673d36de7097349b4495eddd to your computer and use it in GitHub Desktop.
fun requestWidgetPinning(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val componentName = ComponentName(context, YourAppWidget::class.java)
if (appWidgetManager.isRequestPinAppWidgetSupported) {
val pinnedWidgetCallbackIntent = Intent(context, YourAppWidget::class.java)
val successCallback = PendingIntent.getBroadcast(
context, 0,
pinnedWidgetCallbackIntent, 0
)
appWidgetManager.requestPinAppWidget(componentName, Bundle(), successCallback)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment