Skip to content

Instantly share code, notes, and snippets.

@runo280
Created April 20, 2021 04:04
Show Gist options
  • Save runo280/d38dc6bacef3024bfbdb4deb68790be4 to your computer and use it in GitHub Desktop.
Save runo280/d38dc6bacef3024bfbdb4deb68790be4 to your computer and use it in GitHub Desktop.
widget update
class NewAppWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
log("onUpdate")
}
override fun onEnabled(context: Context) {
super.onEnabled(context)
setUpdateAlarm(context)
log("onEnabled")
}
override fun onDisabled(context: Context) {
super.onDisabled(context)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.cancel(getIntent(context))
log("onDisabled")
}
}
fun getIntent(context: Context): PendingIntent =
PendingIntent.getBroadcast(
context, 252,
Intent().setClass(context, TimeChangeReceiver::class.java)
.setAction("com.example.mywidget.APPWIDGET"),
PendingIntent.FLAG_UPDATE_CURRENT
)
fun setUpdateAlarm(context: Context) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = getIntent(context)
log("next min: " + nextMinute().get(Calendar.MINUTE).toString())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC, nextMinute().timeInMillis, intent)
} else {
alarmManager.set(AlarmManager.RTC, nextMinute().timeInMillis, intent)
}
log("setUpdateAlarm")
}
fun log(string: String) {
Log.i("DebugPC", "x0x: $string")
}
fun nextMinute(): Calendar {
return nextMinute(Calendar.getInstance())
}
fun nextMinute(now: Calendar): Calendar {
return nextMinute(now, 1)
}
fun nextMinute(now: Calendar, minutes: Int): Calendar {
val then = now.clone() as Calendar
then[Calendar.SECOND] = 0
then[Calendar.MILLISECOND] = 0
then.add(Calendar.MINUTE, minutes)
return then
}
internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val widgetText = "Time is " + SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.new_app_widget)
views.setTextViewText(R.id.appwidget_text, widgetText)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
log("updateAppWidget")
}
class TimeChangeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
setUpdateAlarm(context)
val updateIntent = Intent(context,NewAppWidget::class.java)
updateIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(context).getAppWidgetIds(ComponentName(context,NewAppWidget::class.java))
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids)
context.sendBroadcast(updateIntent)
log("TimeChangeReceiver onReceive")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment