How to start a foreground service?

4,331 views
Skip to first unread message

steine...@gmail.com

unread,
Apr 16, 2018, 5:34:26 PM4/16/18
to Automate
The start service block doesn't seem to have an option to specify whether it is a background or foreground service and seems to always start it as background service.  How can i launch a foreground service?  Or put differently, how would i do the equivalent of 'adb shell am start-foreground-service -a' compared to 'adb shell am startservice -a ..'.

I want to automate sync for orgzly and that worked fine pre-orea with a start service block but now i get java.lang.IllegalStateException: Not allowed to start service Intent { act=com.orglzly.intent.action.SYNC_START cmp=com.orgzly/.android.sync.SyncService }; app is in background uid null exceptions when trying to start the service ... :-(

-michael-

PS: for more background on my problem see https://github.com/orgzly/orgzly-android/issues/315

Henrik "The Developer" Lindqvist

unread,
Apr 16, 2018, 7:59:59 PM4/16/18
to automa...@googlegroups.com
The Start service block should suffice, since the starting of services in foreground vs regular mode should only affect apps targeting Android 8+, Automate is not, yet.
Automate should always run in a foreground service. If you have the "Hide 'running' notification option" enabled, try disabling it.

Is the IllegalStateException appearing the flow log, or is it the other app that causing it? If it's in the other app, try contacting its developer.

steine...@gmail.com

unread,
Apr 16, 2018, 8:40:06 PM4/16/18
to Automate
Hi Henrik,

Thanks for quick reply (and of course many thanks for automate in the first place! )

The exception is from the flow log, not the other app. I think that app though has upgraded to 8.1 (api 27) as targeted API(min api is 27), around the time the problem started (2018-04-08). Beforehand it did work with that app and on 8.1 device. (Issue started also soon after April security update for 8.1 but that probably is a coincidence) Btw same flow still works with same app on a 7.1 tablet!

And, no, I don't have the hide running option enabled.

-michael-

Btw I've tried also with beta version but didn't make a difference.

steine...@gmail.com

unread,
Apr 16, 2018, 8:43:45 PM4/16/18
to Automate
PS: min api is 16, not 27 as written above but noticed only after submission and couldn't change anymore ..

Henrik "The Developer" Lindqvist

unread,
Apr 16, 2018, 10:22:15 PM4/16/18
to Automate
The other app is targetSdkVersion 27, according to the the git repository you linked.

"Foreground starting" a service assumes that it's a foreground service that will show a foreground/running notification. If not the service app will crash.
https://developer.android.com/reference/android/content/Context.html#startForegroundService(android.content.Intent)

However, as said, Automate should be unaffected:
https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

If it's some new undocumented change, or more like a bug, i'll add a flag to use startForegroundService().

Try using the Shell command block to execute:
am start-foreground-service -a com.orgzly.intent.action.SYNC_START com.orgzly/com.orgzly.android.sync.SyncService


Michael Steiner

unread,
Apr 16, 2018, 11:13:22 PM4/16/18
to Henrik The Developer Lindqvist, Automate
Hi Henrik,

Indeed, via shell command block it works; at least after i realized that i have to add also the option --user 0  (it did work without --user option from adb shell but in the shell bock i got an error 255 and permission denied without that option). Thanks a lot for your help!!

-michael-

--
You received this message because you are subscribed to a topic in the Google Groups "Automate" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/automate-user/MVFbNxvSXUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to automate-user+unsubscribe@googlegroups.com.
To post to this group, send email to automa...@googlegroups.com.
Visit this group at https://groups.google.com/group/automate-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/automate-user/f0539916-5945-4f92-8257-eff3ecdd701e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Henrik "The Developer" Lindqvist

unread,
Apr 16, 2018, 11:36:18 PM4/16/18
to Automate
Odd, --user 0 is root, which should be inaccessible from the Shell command block, unless you used the Shell command superuser block?



On Tuesday, April 17, 2018 at 5:13:22 AM UTC+2, Michael Steiner wrote:
Hi Henrik,

Indeed, via shell command block it works; at least after i realized that i have to add also the option --user 0  (it did work without --user option from adb shell but in the shell bock i got an error 255 and permission denied without that option). Thanks a lot for your help!!

-michael-

steine...@gmail.com

unread,
Apr 16, 2018, 11:51:32 PM4/16/18
to Automate
My phone is not rooted and i used "normal" shell command block, not superuser block.  When i run without --user option, the error was also complaining "Permission Denial: service ask to run as user -2 but is calling from suer 0; this requires android.permission INTERACT_ACROSSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS".  Afaict from googling, these are not linux user ids but android framework ids with 0=USER_OWNER  and -2=USER_CURRENT.

Henrik "The Developer" Lindqvist

unread,
Apr 17, 2018, 1:02:34 AM4/17/18
to Automate
May not work, but in the upcoming Alpha 1.11.1 release i'll include a way to use startForegroundService.
It will be undocumented, for now, setting the Flags input argument to 0x10000000.

Henrik "The Developer" Lindqvist

unread,
Apr 18, 2018, 8:17:04 PM4/18/18
to Automate
Alpha 1.11.1 just released.
Please let me know if the 0x10000000 flag, i.e. startForegroundService, works.



On Tuesday, April 17, 2018 at 5:51:32 AM UTC+2, steiner.michi wrote:

2kyle...@gmail.com

unread,
Jun 16, 2018, 4:13:04 PM6/16/18
to Automate
Just wanted to chime in that I was unable to start a foreground service on my Android P device (if it hadn't been previously activated). Adding this flag worked perfectly. Thanks!
Reply all
Reply to author
Forward
0 new messages