Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid BroadcastReceiver within Activity
    primarykey
    data
    text
    <p>I'm just trying this little sample project, all it does: Activity one has a Button that sends a Broadcast. Activity two displays a toast when received. Below is the code, the Broadcast is never received. What do I do wrong?</p> <p><em>Sending the Broadcast</em></p> <pre><code>public class SendBroadcast extends Activity { public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void sendBroadcast(View v){ Intent broadcast = new Intent(); broadcast.setAction(BROADCAST_ACTION); sendBroadcast(broadcast); } } </code></pre> <p><em>Receiving it</em></p> <pre><code>public class ToastDisplay extends Activity { private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT); } }; @Override protected void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction(SendBroadcast.BROADCAST_ACTION); registerReceiver(receiver, filter); super.onResume(); } @Override protected void onPause() { unregisterReceiver(receiver); super.onPause(); } } </code></pre> <p>Manifest</p> <pre><code>&lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".SendBroadcast" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".ToastDisplay"&gt; &lt;intent-filter&gt; &lt;action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"&gt;&lt;/action&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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