When the widget ids are already stored in Android framework, on boot up Homescreen uses the existing ids and calls addView to load the widget. But from the addView call, it takes 14 sec to get the android.appwidget.action.APPWIDGET_ENABLED and android.appwidget.action.APPWIDGET_UPDATE broadcasts in Appwidget provider class. Due to this the widget gets loaded with delay in Homescreen.
Could you please help to understand how to optimize the loading time? The following code is used to add the view on start up-
private void loadWidgets(WidgetInfo info, FrameLayout
frameLayoutContainer, AppWidgetHostView view) {
if (info.getAppWidgetInfo() != null) {
frameLayoutContainer.removeAllViews();
if (view.getParent() != null) {
((ViewGroup) view.getParent()).removeView(view);
}
view.setPadding(WIDGET_PADDING, WIDGET_PADDING, WIDGET_PADDING, WIDGET_PADDING);
frameLayoutContainer.addView(view);
frameLayoutContainer.invalidate();
} else {
frameLayoutContainer.removeAllViews();
frameLayoutContainer.invalidate();
frameLayoutContainer.setBackgroundColor(Color.TRANSPARENT);
}
}
private void bindWidget(WidgetInfo info) {
if (info.getAppWidgetInfo() == null || info.getAppWidgetInfo().provider == null) {
return;
}
try {
AppWidgetManager widgetManager = AppWidgetManager
.getInstance(requireContext().getApplicationContext());
boolean bindWidgetStatus = widgetManager
.bindAppWidgetIdIfAllowed(info.getWidgetId(), info.getAppWidgetInfo().provider);
} catch (Exception e) {
Log.e(TAG, "bindWidget Unexpected error: " + e.getMessage());
e.printStackTrace();
}
}
In case of fresh start, when there is no widget id stored in framework, it calls bindAppWidgetIdIfAllowed. When the ids are already present, it calls addView. My understanding is that the broadcasts are sent based on bindAppWidgetIdIfAllowed or addView. How can we get the broadcast faster?
Some of the widget providers are implemented as part of Homescreen app
For those, do not use the app widget framework. Just have them be part of your launcher ("Homescreen") app. You gain nothing from using the app widget framework and have to deal with lots of limitations.
some of them belong to different apps. This is done for Automotive android.
Android Automotive (AAOS) should allow you, or your car manufacturer partner, the ability to modify the OS. In effect, every car model gets a custom build of AAOS. Whoever is creating that custom build of AAOS could investigate where broadcasts get throttled and look into adjusting those settings.
That being said, please bear in mind that a lot of car infotainment hardware is not very good, similar to how Android TV devices have limited CPUs and RAM. The reason why broadcasts get throttled is to ensure smooth-but-sometimes-delayed behavior, rather than having a lot of jank because lots of processes have to be forked and given CPU/RAM in rapid succession. Personally, I would leave this alone.