0

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?

Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45
Akanksha Hegde
  • 1,738
  • 11
  • 14

2 Answers2

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