Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReceiverRestrictedContext cannot be cast to android.app.Activity

I need an Activity in my BroadcastReceiver, but when i try to cast the context to activity, it doesn't work.

Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity

How can I get Activity from BroadcastReceiver ?

Thanks

like image 534
MohamedAli Avatar asked Apr 23 '15 11:04

MohamedAli


1 Answers

How can I get Activity from BroadcastReceiver ?

Generally, you can't. A BroadcastReceiver is independent from an Activity. For example, you can have BroadcastReceiver which receive broadcasts when no Activity is running.

You can, however, register/unregister a BroadcastReceiver in an Activity and use it to receive broadcasts while your Activity is running.

like image 162
cygery Avatar answered Oct 19 '22 03:10

cygery