• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with Broadcast receiver

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Thanks.
 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any thoughts on this one please.
 
Ranch Hand
Posts: 105
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Cheers,

Johan
 
This is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic