Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service started from widget doesn't work in Oreo

Tags:

android

widget

When I implemented my widget a couple years ago, I used this advice from the App Widgets page on the development site (still there): "If your App Widget setup process can take several seconds (perhaps while performing web requests) and you require that your process continues, consider starting a Service in the onUpdate() method." I did this and it worked like a charm when built for SDKs up through 25.

It also worked on Android 8.0, but as soon as I targeted 26, the app started crashing in the onUpdate method of the widget provider when issuing the startService call. The error message had to do with trying to start a background service from another background service, and although I tried a few things in the service definition in the manifest I couldn't get it working.

I ended up doing a workaround when I decided that I didn't really need to update the widget from a service, so now just do the update directly in onUpdate. Services and background/foreground issues are something I've not messed with much, but I'd still like to know if there is a way to have kept using a service to update the widget.

like image 354
gordonwd Avatar asked Jan 04 '18 21:01

gordonwd


People also ask

Why is my phone widget not working?

Long-press a widget or the app corresponding to the widget. Tap on App Info and select Storage & cache from the options. Try clearing the cache first and see if it fixes the issue. If not, clear the app data as well.


2 Answers

in Android O, we have a new background limitations. When you're trying to startService(), you will get IlleagalStateException, so now you should use startForegroundService(), but if you start service by this new method, you will get RemoteServiceException. To avoid this exception you have 5 seconds to make startForeground() after startForegroundService(), to notify user, that you're working in background.

So, where is only one way in Android O:

context.startForegroundService(intent)

And in service onCreate() make something like that:

startForeground(1, new Notification());

UPDATE So as i assume, some manufacturers have backport on Android N these new background limitations, that's why you can get same exception in Android N.

like image 103
HeyAlex Avatar answered Oct 02 '22 18:10

HeyAlex


Without a Minimal, Complete, and Verifiable example, it is difficult to help you. Depending on what you had, you could have:

  • Switched to JobIntentService, particularly if your old service was an IntentService and you did not mind the possibility of a several-second delay in the work being started

  • Called startForegroundService() on Context (instead of startService()) to start your service, and in your service call startForeground(), to make your service be a foreground service

  • Called getForegroundService() on PendingIntent (instead of getService()), and in your service call startForeground(), to make your service be a foreground service

like image 27
CommonsWare Avatar answered Oct 02 '22 18:10

CommonsWare