0

So i have a class that listens to incoming calls, i want to make a service to all my app to receive calls even when i'm not in the application's UI. How to trigger this class(broadcast receiver). I used "sendBroadcast" and have a FC.

sendBroadcast(new Intent(context, IncomingCallReceiver.class));

Thank you for your help.

androniennn
  • 3,117
  • 11
  • 50
  • 107

1 Answers1

1

In your case I would use the following approach:

  • Create Service and start it from your Activity (you mentioned that you have several applications, so starting first of them may also start the Service).
  • Make sure that Service does not work forever, so stop the service when you do not need it any more (last of your applications is finished). Service may terminate self even without Activity by calling stopSelf(). Please note that also System may terminate your Service and prevent it from working forever.
  • Make private class within Service that extends BroadcastReceiver and register it for the Intents you want to monitor using Service function registerReceiver().
  • Once you receive wanted Intent, you can call some Service function from within BroadcastReceiver onReceive(). For example, you may sendBroadcast() using some custom Intent that is recognized by your applications.
  • When Service is stopped make sure that you un-register the BroadcastReceiver extension using Service function unregisterReceiver().

UPDATE:

Community
  • 1
  • 1
Zelimir
  • 11,008
  • 6
  • 50
  • 45