Android BroadcastReceiver in action

I just try this small sample of the project, all that it does: In the first operation there is a button that sends the broadcast. The second operation shows a toast when it is received. Below is the code, broadcast not received. What am I doing wrong?

Broadcast Submission

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); } } 

Getting

 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(); } } 

manifest

 <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SendBroadcast" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ToastDisplay"> <intent-filter> <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"></action> </intent-filter> </activity> </application> 
+46
android broadcastreceiver
Dec 29 '10 at 15:09
source share
7 answers

What am I doing wrong?

The source code of ToastDisplay is fine (mine is similar and works), but it will only get something if it is currently in the foreground (you register the receiver in onResume). But it cannot receive anything if another action is displayed (in this case, the SendBroadcast action).

Instead, you probably want to startActivity ToastDisplay from the first action?

BroadcastReceiver and Activity make sense in another use case. In my application, I need to receive notifications from the GPS tracking service and show them in action (if the action is in the foreground ).

There is no need to register the recipient in the manifest . It would be even malicious in my use case - my receiver manipulates the user interface of the action and the user interface will not be available during onReceive if activity is not currently displayed. Instead, I register and unregister the recipient for activity in onResume and onPause, as described in the BroadcastReceiver Documentation :

You can dynamically register an instance of this class using Context.registerReceiver () or statically publish the implementation via a tag in your AndroidManifest.xml.

+40
May 09 '11 at 16:09
source share
β€” -
  Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT); 

makes a toast but does not show it.

You need to do Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();

+37
Sep 06 2018-11-11T00:
source share

You need to define the receiver as a class in the manifest, and it will get the intention:

 <application .... <receiver android:name=".ToastReceiver"> <intent-filter> <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/> </intent-filter> </receiver> </application> 

And you do not need to create the class manually inside ToastDisplay.

In the code you specified, you must be inside the ToastDisplay operation in order to actually get the intent.

+4
Dec 29 '10 at
source share

Extends the ToastDisplay class with BroadcastReceiver and registers the receiver in the manifest file and does not register your broadcast receiver in onResume ().

 <application .... <receiver android:name=".ToastDisplay"> <intent-filter> <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/> </intent-filter> </receiver> </application> 

if you want to register in action, then register in the onCreate () method, for example:

 onCreate(){ sentSmsBroadcastCome = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "SMS SENT!!", Toast.LENGTH_SHORT).show(); } }; IntentFilter filterSend = new IntentFilter(); filterSend.addAction("m.sent"); registerReceiver(sentSmsBroadcastCome, filterSend); } 
+4
Apr 10 '12 at 7:26
source share

I think your problem is that you are broadcasting before starting another activity! therefore, other activities will not receive anything.

  • The best practice for testing code is to send a broadcast from a stream or from a service so that the activity is open and its registered recipient and background process send a message.
  • run the ToastDisplay action from sender activity (I have not tested this, but may work)
+1
Aug 19 '11 at 19:51
source share

You forget to write .show () at the end, which is used to display the toast.

 Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show(); 

This is a common mistake that the programmer makes, but I’m sure that after that you will no longer repeat the error ...: D

+1
May 03 '16 at 16:33
source share

You also need to register the receiver in onCreate (), for example:

 IntentFilter filter = new IntentFilter(); filter.addAction("csinald.meg"); registerReceiver(receiver, filter); 
-one
Nov 09 '14 at 16:52
source share