-1

I want to implement some kind of ContentObserver and have maximum guarantee that it will run in the background and respond to change events when they occur. What is the best way to do it ? I do not want to have accompanying running Activity to keep Service alive. I need different elegant approach and the priority is the "service" or "background thread" will run continuously or at least continuously respond to change events. Something like BroadcastReceiver but my ContentObserver is unable to be defined in manifest.

tomcatlxv
  • 1
  • 1
  • 1
    What have you tried so far? What research have you done? – aliaksei Jan 11 '18 at 16:29
  • @aliaksei I read a lot about background services but in new androids they appear not to be reliable (they stop when starting component dies). I need activity independent long running task responding to ContentObserver events (not Broadcast which seems to be easier by defining it in manifest) – tomcatlxv Jan 11 '18 at 16:33

2 Answers2

1

On Android 7.0+, use addTriggerContentUri() on JobInfo.Builder. This will allow you to have a JobService that gets control when content at the Uri changes, without having a continuously-running service.

On Android 6.0 and older, use a foreground service.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you. I am interested only in 7+ so addTriggeredContentUri() seems to be attractive. But can I be sure that JobScheduler will react fast enough to catch every such event ? I mean all the stuff connected with battery optimization etc. If for example I want to catch content changing event and react reading the content in one-two seconds ? Or be sure to read the content before next event occurs ? – tomcatlxv Jan 11 '18 at 17:03
  • @tomcatlxv: " If for example I want to catch content changing event and react reading the content in one-two seconds ?" -- you have no way of guaranteeing that by any means. "Or be sure to read the content before next event occurs ?" -- you have no way of guaranteeing that by any means either. – CommonsWare Jan 11 '18 at 17:18
  • What is a typical time JobScheduler will react on this type of trigger ? If I implement the program different less elegant way as an activity it probably will react in "real time". So maybe foreground service would be better even for 7+ androids ? I think it must be possible as such applications like sms or phone app do react in "real time". How they are implemented ? I cannot give you a star since I am too new on SO :) Anyway thanks. – tomcatlxv Jan 11 '18 at 17:51
  • @tomcatlxv: "What is a typical time JobScheduler will react on this type of trigger ?" -- seconds, AFAIK, though it will vary based on how busy the device is at the time. "it probably will react in "real time"" -- not necessarily. "How they are implemented ?" -- they do not need a `ContentObserver` in general. You need a `ContentObserver` for observing foreign providers, not your own. – CommonsWare Jan 11 '18 at 17:54
  • Will Broadcast Receiver for incoming sms react faster in general than CO ? If so what to do with outgoing sms better than ContentObserver (in tems of efficiency) ? – tomcatlxv Jan 11 '18 at 18:12
  • "Will Broadcast Receiver for incoming sms react faster in general than CO ?" -- well, yes, by definition. The SMS message has not been stored in the database by the time the broadcast arrives. It is the job of the SMS client to store the message in the database. "If so what to do with outgoing sms better than ContentObserver (in tems of efficiency) ?" -- write a full-featured SMS client app and have users use that. – CommonsWare Jan 11 '18 at 18:20
  • Assume I will use JobScheduler for ContentObserver notification catching. This is a one shot task in this case. How (where ?) to immediately schedule next JS task one to avoid any inactivity period in this service ? I mean how to effectively ensure continuous observation of CO by means of JS ? – tomcatlxv Jan 16 '18 at 12:38
  • @tomcatlxv: "How (where ?) to immediately schedule next JS task one to avoid any inactivity period in this service ? " -- you don't. "how to effectively ensure continuous observation of CO by means of JS ?" -- you don't. That is not what `JobScheduler` is for. – CommonsWare Jan 16 '18 at 12:43
  • So what they (AndroidDevelopers) mean by: "To continually monitor for content changes, you need to schedule a new JobInfo observing the same URIs before you finish execution of the JobService handling the most recent changes. Following this pattern will ensure you do not lost any content changes: while your job is running, the system will continue monitoring for content changes, and propagate any it sees over to the next job you schedule." – tomcatlxv Jan 16 '18 at 12:53
  • @tomcatlxv: I see what you are referring to. You would schedule it from within your `JobService`, as part of the code where you are handling the job. – CommonsWare Jan 16 '18 at 12:54
-1

This is only part of the answer start app on boot complete I assume you want something to launch when boot completes a service maybe? Anyway hope this helps