I have a service which will broadcast a message to broadcast receiver which is registered in some activity. The activity might be running or not. If it's open then broadcast will be received. But if not, the broadcast will be wasted. Is there a way to avoid this broadcast from being wasted without adding the broadcast inside manifest?
Asked
Active
Viewed 71 times
0
-
Are you using local broadcast? – Ronak Thakkar Jun 01 '17 at 10:31
-
broadcast will be wasted? So you do not want to receive event while the activity is not running. !? – Paresh P. Jun 01 '17 at 10:32
-
Use service binding instead(for comunication) – Selvin Jun 01 '17 at 10:33
-
There is a service that is sending broadcasts. So if if activity is not running I want to receive the broadcast and not waste it. Is there a way to do that without registering broadcast in manifest? – Akanksha Hegde Jun 01 '17 at 10:34
-
So you don't want to unregister broadcast after finishing the activity? – Ronak Thakkar Jun 01 '17 at 10:36
-
If I unregister then I cannot receive the broadcast when activity is background right? Is there a workaround for it? – Akanksha Hegde Jun 01 '17 at 10:40
2 Answers
1
Consider to start/stop that service based on Activity's lifecycle, when Activity is created (onCreate()) start that service. When Activity is destroyed (onDestroy()) stop the service so you avoid "waste" those messages and even have a service running.
AlexTa
- 5,133
- 3
- 29
- 46
0
1- Create an inner class in your activity that extends BroadcastReceiver.
2- Create an object of your receiver and an intent filter.
3- Override onResume and registerReceiver(receiver, intentFilter);
4- Override onPause and unregisterReceiver(receiver);
Take a look at this answer. you dont have to add it in the manifest. answer here
Tahir Ferli
- 636
- 4
- 16