I have a widget that has network access.
Updating widgets in power saving mode results in a timeout exception.
It turns out that handling the battery optimization exception will resolve it.
But some apps have seen network access in widgets without any battery optimization exceptions.
How are they possible?
Here is my subclass of AppWidgetProvider.
public class WidgetProvider4x2 extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
requestApi();
}
}
private void requrestApi() {
// timeout occurs
}
}
That's because when power saving mode on makes RESTRICT_BACKGROUND_STATUS_ENABLED is true. When RESTRICT_BACKGROUND_STATUS_ENABLED is true we can't load/fetch data from network outside view layer eg. activity/fragment. To solve this you can request data restriction permission using intent.