It's not a secret anymore!
The moose likes Android and the fly likes Problems with Broadcast receiver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Problems with Broadcast receiver" Watch "Problems with Broadcast receiver" New topic

Problems with Broadcast receiver

Rakesh Jhamb
Ranch Hand

Joined: Jun 18, 2003
Posts: 154

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.


Rakesh Jhamb
Ranch Hand

Joined: Jun 18, 2003
Posts: 154
Any thoughts on this one please.
Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Posts: 105

Hi Rakesh,

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

Some examples of both types of broadcast messages: Do something when the phone rings and Process outgoing calls



Johan Pelgrim, The Netherlands
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
I agree. Here's the link:
subject: Problems with Broadcast receiver
It's not a secret anymore!