Li Che
Li Che

Reputation: 737

Call a method in Activity of BroadcastReceiver

I transfer context into a Specific Activity in onReceive()of BroadcastReceiver, like MyActivity ma = (MyActivity) context;. I want to call a method in Activity, but when I transfer context, it has this error: ClassCastException error. Before, in another project, I transferred the service without problem. Why does it give an error?

Upvotes: 1

Views: 222

Answers (1)

ashakirov
ashakirov

Reputation: 12350

because context variable in onReceive method (docs) is The Context in which the receiver is running. ( you can't cast it to activity because receiver is running in application context, I think.)

You can organize communication between application components via handlers or via broadcasting custom intents.

Upvotes: 2

Related Questions