Skip to content

Instantly share code, notes, and snippets.

@Ayush783
Created July 15, 2024 07:15
Show Gist options
  • Save Ayush783/286b220cfe480716b902f879995723d4 to your computer and use it in GitHub Desktop.
Save Ayush783/286b220cfe480716b902f879995723d4 to your computer and use it in GitHub Desktop.
class ListViewAppWidgetProvider : HomeWidgetProvider() {
...
companion object {
fun createListView(context: Context, widgetId: Int, data: String): RemoteViews {
val intent = Intent(
context,
ListViewWidgetService::class.java
)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId)
intent.putExtra("ARTICLES_JSON", data)
val views = RemoteViews(context.packageName, R.layout.listview_app_widget)
views.setRemoteAdapter(R.id.listview_app_widget, intent)
val pendingIntentWithData = getPendingIntent(
context,
MainActivity::class.java
)
views.setPendingIntentTemplate(R.id.listview_app_widget, pendingIntentWithData)
return views
}
private fun getPendingIntent(context: Context, activityClass: Class<MainActivity>) : PendingIntent {
val intent = Intent(context, activityClass)
intent.action = "es.antonborri.home_widget.action.LAUNCH"
var flags = PendingIntent.FLAG_UPDATE_CURRENT
if (Build.VERSION.SDK_INT >= 23) {
flags = flags or PendingIntent.FLAG_MUTABLE
}
return PendingIntent.getActivity(context, 0, intent, flags)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment