0

after an extensive search I still can't find a solution to my problem:

I need a Broadcast that runs once a day, no matter if the App is running or not. However, IF the App is running, I also need to update the UI at the end of/after the Broadcast.

I can't use a programmatically registered Broadcast because it ends with the Apps lifecycle. But from a static manifest-registered Broadcast I can't access the UI (at-least I don't know HOW). One option would be to have 2 different Broadcasts and cancel/start them in onPause and onResume, but I wonder if there is an easier solution?

Deee
  • 331
  • 2
  • 9
  • 27
Florian Walther
  • 6,237
  • 5
  • 46
  • 104
  • cannot you run a `Service` once a day, instead of `BroadcastReceiver`? – pskink Sep 01 '17 at 06:42
  • Oops i didnt look into Services yet, since i am a beginner and i was told that Broadcasts are what i am looking for. I will immediatly look up Services. – Florian Walther Sep 01 '17 at 06:43
  • and check [bound services](https://developer.android.com/guide/components/bound-services.html) – pskink Sep 01 '17 at 06:44
  • Have a look [here](https://stackoverflow.com/questions/28083430/communication-between-broadcastreceiver-and-activity-android). Otherwise - as already said - have a look into services. You can bind to services to exchange data (look @pskink's answer) – Ch4t4r Sep 01 '17 at 06:46
  • Which broadcast intent you are trying to register to?With version O, Android limits explicit broadcast. You may want to check the Background execution limits here https://developer.android.com/guide/components/broadcast-exceptions.html – Swati Sep 01 '17 at 06:48
  • One of the links recommends Observables. I dont know that yet, but it seems like a simple solution. Would you guys recommend i use Service or the Observable solution? – Florian Walther Sep 01 '17 at 06:53
  • I looked into services, but whenever i search for "run service every xx hour", they recommend using an alarmmanager and i am where i started out. I think for now ill try using Obervables. – Florian Walther Sep 01 '17 at 23:56
  • I just wanna let you know that Observers was a very easy and working solution. Ch4t4r, maybe you wanna post this as an answer so i can accept it? – Florian Walther Sep 02 '17 at 01:10

1 Answers1

1

The thing you need is not broadcast receiver along with AlarmManager or JobScheduler for api above 21 and greenrobot event bus.

AlarmManager Schedules the broadcast call once a day or at any time you want and every time if the broadcast is called you can trigger event from eventbus and receive that event in the place where you want it. The thing why to use event bus is we do not need to handle if the view is visible or not.iF the view is in re use state it triggers the event the view and one method is called by event bus and in that method you can do anything you want to do with view.

personally i don't prefer service because service execution is really expensive now a days.

Note: the package name where you put alarm manager and broadcast receiver should be "alert" some samsung mobile are very optimized so they will only let the package name with "alert to run fully". You will also need on boot receiver to register receiver and schedule alarmmanager in case if the phone is booted.

taman neupane
  • 938
  • 9
  • 18
  • Thank you, seems like a good solution. About your note: Do you mean, the keyword "alert" has to be in the package name of my whole app (which you set at the very beginning), i just in an underlying folder? – Florian Walther Sep 01 '17 at 07:02
  • This is not the official solution but i was facing problem with s7 edge. And found it to be working. The Context was not same as yours but it was similar. No the package containing alarmmanager and broadcast receiver for view change should be seperate package called "alert". It is prefered to keep bootcomplete receiver to the root of your package. – taman neupane Sep 01 '17 at 07:11
  • Thank you. But what would i do if the the Receiver is an inner class in the MainActivity? Should i put the whole MainActivity into the alert folder? – Florian Walther Sep 01 '17 at 07:12
  • It is good to seperate out module. And place receiver in seperate module. – taman neupane Sep 01 '17 at 07:17
  • Thank you. If i switch from BroadcastReceiver to a Service, will there be a similar problem with Samsung Phones? – Florian Walther Sep 01 '17 at 07:29
  • ok you can use intent service(Single run). Alarm manager pending intent will be broadcast receiver and it will start service. And in service will trigger event using greenrobot eventbus to required view. – taman neupane Sep 01 '17 at 08:47