9

I want to use app = (MyApplication)getApplication(); in BroadcastReceiver

i am using thi link: http://www.devahead.com/blog/tag/application/

but getApplication not exits in BroadcastReceiver, How can i use it?

I need it becouse i want to share data between two BroadcastReceiver

senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • I don't think it's possible, because afaik `BroadcastReceiver` doesn't have context. And you can only `getApplication()` when you have a `Context`. Maybe try to write what exactly you're trying to achieve, it'll be easier to find another solution – Michał Klimczak Aug 16 '12 at 20:21
  • thank you. But i am trying to sned property data from one receiver to another. I want that some code in receiver 1 will not execute if rceiver 2 didn't do someething... – senzacionale Aug 17 '12 at 07:41

1 Answers1

2

Your BroadcastReceiver has an onReceive(Context ctx, Intent intent) method which is called to receive a message... you're being hand-delivered a context.

mah
  • 39,056
  • 9
  • 76
  • 93
  • thank you. I am reading this http://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext which said that it is not necessary that i will get application instance. So what i am trying to do. I am trying to sned property data from one receiver to another. I want that some code in receiver 1 will not execute if rceiver 2 didn't do someething... is this correct way with application? – senzacionale Aug 17 '12 at 07:46
  • 1
    You may not receive an application context directly however you do not care since you can call `ctx.getApplicationContext()` to get one. As to the specifics of what you're trying to accomplish, there are many ways to solve that and the "right" one depends on factors which are well beyond the scope of your question or even an appropriate question for stackoverflow. Some possibilities though include simple SharedProperty values, Singleton class methods, a local service to set/get properties, and multiple broadcast receivers; there is no one right answer. – mah Aug 17 '12 at 11:31
  • 3
    But `getApplicationContext()` is not `getApplication()`. Isn't it possible that `(Application)getApplicationContext()` will fail? – Timmmm Nov 03 '12 at 13:24
  • @Timmmm it depends on what it is you actually need. The OP just needed a common context (not that Application, like Activity and other classes, extends indirectly from Context). If you truly need your Application class then this will not work and you would need to resort to a less elegant method if you're in a class that cannot get the application directly. – mah Nov 03 '12 at 13:41
  • Yeah what I am planning to do for my messaging app is have two `BroadcastReceivers` - one that handles messages while my app isn't running and creates the notifications. That one is declared in the manifest. And then I have another one that is registered programmatically from my `Application` and updates the activities as needed. – Timmmm Nov 03 '12 at 14:00
  • @Timmmm Does your approach has worked for you? I will probably do the same. I guess that getApplicationContext() should work to get the Application, right? – sockeqwe Jul 03 '13 at 19:39
  • `app = (MyApplication)context.getApplicationContext();` worked for me. – ban-geoengineering Aug 08 '14 at 22:21