版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
1、简单的创建完成后:
在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) { onAppWidgetOptionsChanged( context, appWidgetManager, appWidgetId, appWidgetManager.getAppWidgetOptions(appWidgetId) ) } }
这个方法系统会根据你的配置文件进行周期性调用,虽然说最少是15分钟,但是经常性是30分钟调用一次。
/** * 桌面小部件添加时调用 * @param context receiver is running */ override fun onEnabled(context: Context) { startNotifyServiceOne() val componentName = ComponentName(context, WeatherOneWidget::class.java) val packageManager = context.packageManager val componentEnabledSetting = packageManager.getComponentEnabledSetting(componentName) if (componentEnabledSetting != PackageManager.COMPONENT_ENABLED_STATE_ENABLED && componentEnabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { packageManager.setComponentEnabledSetti