I am facing following 2 problems with BroadCastReceiver:
1) I want to save state between different invocations in my receiver to do some comparison from invocation 1 to invocation 2. So I created an instance variable in my Receiver which will hold the boolean state. But it looks like a new instance of my receiver is created every time, so my state is lost. What is the best way to save state in between multiple invocations of BroadCastReceiver.
2) I want to run a long running task when my receiver is invoked, for example, when the phone rings. But we cannot run long running tasks the way receivers are implemented. How can I implement such functionality.
On 1) Could you maybe elaborate a bit more what broadcast message you are listening to and why you would want to hold some sort of state? If the broadcast message is a normal broadcast message you cannot depend on which broadcast receiver gets the message first. So invocation 2 could come before invocation 1 (in your example). If you are working with ordered broadcast messages you can influence the order via the android:priority attribute on the <intent-filter> element in your AndroidManifest.xml file. And if you really want maybe you can put the state on the Application object instead? Or use SharedPreferences