I have home screen widget, which has a simple AppWidgetProvider
and JobIntentService
, where I do all the work.
Problem is - it works kind of randomly. Sometimes it does, sometimes it doesnt - weirdest thing is, I can see in the log, that on each widget update enqueueWork
method of JobIntentService
is ALWAYS called, but onHandleWork
method only sometimes.
(I have found there is strong, though not 100% correlancy with battery optimization. If I turn of "Manage apps automatically", then it 99% works reliably. If it is turned on, its like flipping a coin. Sometimes it does, sometimes it doesnt. Best to ilustrate behavior would be this short simple video
This is my code (Widget provider):
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.v("aaa", "onUpdate");
// super.onUpdate(context, appWidgetManager, appWidgetIds);
// update in my own Service (to avoid ANR)
Intent intent = new Intent(context, MyJobIntentService.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
MyJobIntentService.enqueueWork(context, intent);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v("aaa", "onReceive");
super.onReceive(context, intent);
}
}
And this is my service (JobIntentService) where I do all work:
public class MyJobIntentService extends JobIntentService {
public static final int JOB_ID = 1;
public static void enqueueWork(Context context, Intent work) {
Log.v("aaa", "enqueueWork: ");
enqueueWork(context, MyJobIntentService .class, JOB_ID, work);
}
@Override
protected void onHandleWork(Intent intent) {
Log.v("aaa", "onHandleWork: ");
}
}