'Android broadcast receiver when the app is in background

I'm facing a problem with the android broadcast receiver. I have a login page and after a successful login the app goes to another activity. And it contains a broadcast receiver. I am sending a broadcast once a separate service completes its action. The app works fine in a happy day scenario.

My problem here is, after i entered the username / password and hit the login button i send the app to the background. But when the app is in background that broadcast receiver wont receive the broadcast which i'm sending.

Anyone have a clue regarding this issue ?

Any help would be greatly appreciated. Thanks in advance.



Solution 1:[1]

Is you start separate service in another Activity right after you hit the login button ? Because service is the entity that ment to run on the background.

If your answer is yes, i think you should consider to check the lifecycle of the activity, because there is possibility that when you hit the login button, you send your application on the background. Now, at this point activity inserts to onPause. Therefore your second activity is not active and as a result can't call to the service.

I hoped it will you help to resolve your problem :)

Solution 2:[2]

Are you unregistered the broadcast receiver in any of the activities onPause/OnStop/OnDestroy method? if yes then you will not receive the broadcast. make sure you have register the Broadcast receiver in the AndroidManifest. and not unregistered. if you wanted to receive intent in the background then you should not unregistered it. 0

If you start separate service in another Activity right after you hit the login button ? Because service is the entity that ment to run on the background.

If your answer is yes, i think you should consider to check the lifecycle of the activity, because there is possibility that when you hit the login button, the application will be in background. Now, at this point activity inserts to onPause(). Therefore your second activity is not active and as a result can't call to the service.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jviaches1
Solution 2 Spiker