I'm trying to make a widget refresh button. I found this code, it works great on android 11. But on version 12 and higher, it breaks the application. What could be the problem?
// onReceive function to process clicks
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (intent.action == AppWidgetManager.ACTION_APPWIDGET_UPDATE) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)
if (appWidgetIds != null) {
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}
}
}
// add PendingIntent
val updateWidgetIntent = Intent(context, NextLectureWidget::class.java)
updateWidgetIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(appWidgetId))
val updateWidgetPendingIntent = PendingIntent.getBroadcast(
context, 0, updateWidgetIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
remoteViews.setOnClickPendingIntent(R.id.updateButton, updateWidgetPendingIntent)