Note that there are some explanatory texts on larger screens.

plurals
  1. POBroadcastReceiver performing different actions based on Activity running
    primarykey
    data
    text
    <p>I'm wondering if there is a way to determine the active activity during the <code>onReceive(Context context, Intent intent)</code> method of a <code>BroadCastReceiver</code>? I initially thought that the context passed in was such a way, but later found out that in fact it is a <code>ReceiverRestrictedContext</code>. </p> <p>I'm trying to do the following similar to how one passes information from a <code>Fragment</code> to its encapsulating <code>Activity</code>:</p> <pre><code>try { mListener = (MyListenerInterface) (context.getApplicationContext()); } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement MyListenerInterface"); } mListener.onMessageReceived(msg); </code></pre> <p>Each activity implements MyListenerInterface, and overrides the onMessageReceived(msg) method, which I thought would be enough. However, I am receiving the following exception when running and receiving a broadcast:</p> <pre><code>09-24 22:40:48.706: E/AndroidRuntime(29267): FATAL EXCEPTION: main 09-24 22:40:48.706: E/AndroidRuntime(29267): java.lang.RuntimeException: Unable to start receiver com.test.stuff.Receiver: java.lang.ClassCastException: android.app.ReceiverRestrictedContext@448c8008 must implement MyListenerInterface 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821) 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.app.ActivityThread.access$3200(ActivityThread.java:125) 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083) 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.os.Handler.dispatchMessage(Handler.java:99) 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.os.Looper.loop(Looper.java:123) 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-24 22:40:48.706: E/AndroidRuntime(29267): at java.lang.reflect.Method.invokeNative(Native Method) 09-24 22:40:48.706: E/AndroidRuntime(29267): at java.lang.reflect.Method.invoke(Method.java:521) 09-24 22:40:48.706: E/AndroidRuntime(29267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 09-24 22:40:48.706: E/AndroidRuntime(29267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 09-24 22:40:48.706: E/AndroidRuntime(29267): at dalvik.system.NativeStart.main(Native Method) 09-24 22:40:48.706: E/AndroidRuntime(29267): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext@448c8008 must implement MyListenerInterface 09-24 22:40:48.706: E/AndroidRuntime(29267): at com.test.stuff.Receiver.onReceive(Receiver.java:63) 09-24 22:40:48.706: E/AndroidRuntime(29267): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810) 09-24 22:40:48.706: E/AndroidRuntime(29267): ... 10 more </code></pre> <p>At this point I am just thinking about making an inner class <code>BroadcastReceiver</code> in each activity and putting the desired functionality in those, but I am wondering if anyone knows if any work-around to the above scenario where I would only need one <code>BroadcastReceiver</code> which would have different functionality based on the running Activity.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload