0

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
    }
}
thesquare12
  • 23
  • 1
  • 6
  • Does requrestApi() open a new thread? – Gil Becker Sep 08 '20 at 11:44
  • @GilBecker Does that have an effect? It uses AsyncTask in my case. – thesquare12 Sep 08 '20 at 14:45
  • No, I don't think so. I managed to reproduce your issue, but not solve it. Not sure why battery saver causes these Volley timeout exceptions. Try to do something similar to this: https://stackoverflow.com/questions/56988709/how-to-fix-com-android-volley-timeouterror-in-android-while-trying-to-perform Maybe changing the retry policy might help... – Gil Becker Sep 09 '20 at 16:20

1 Answers1

2

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.

Intent intent = new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS, Uri.parse("package:" + getPackageName()));
            startActivity(intent);
ilham suaib
  • 272
  • 3
  • 10