Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

localbroadcastmanager android example

I'll answer this anyway. Just in case someone needs it.

ReceiverActivity.java
An activity that watches for notifications for the event named "custom-event-name".

@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Register to receive messages.
  // We are registering an observer (mMessageReceiver) to receive Intents
  // with actions named "custom-event-name".
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Get extra data included in the Intent
    String message = intent.getStringExtra("message");
    Log.d("receiver", "Got message: " + message);
  }
};

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}
SenderActivity.java
The second activity that sends/broadcasts notifications.

@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Every time a button is clicked, we want to broadcast a notification.
  findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      sendMessage();
    }
  });
}

// Send an Intent with an action named "custom-event-name". The Intent sent should 
// be received by the ReceiverActivity.
private void sendMessage() {
  Log.d("sender", "Broadcasting message");
  Intent intent = new Intent("custom-event-name");
  // You can also include some extra data.
  intent.putExtra("message", "This is my message!");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
With the code above, every time the button R.id.button_send is clicked, an Intent is broadcasted and is received by mMessageReceiver in ReceiverActivity.

The debug output should look like this:

01-16 10:35:42.413: D/sender(356): Broadcasting message
01-16 10:35:42.421: D/receiver(356): Got message: This is my message! 
Comment

PREVIOUS NEXT
Code Example
Java :: get week number from localdate java 
Java :: how to make a rest api in spring 
Java :: fix nullpointerexception 
Java :: how to compare doubles in java 
Java :: java exception override message 
Java :: java variable 
Java :: swapping two numbers using third variable 
Java :: processing pi 
Java :: android studio int ot string 
Java :: how to find a word in a statement java 
Java :: getstring java 
Java :: java dictionary initialization 
Java :: javax dependency android 
Java :: Write a method multiply() in a class Arithmetic 
Java :: Java Access Array Elements 
Java :: java how to split a string to array 
Java :: android click button programmatically 
Java :: multiple inheritance in java 
Java :: single level inheritance in java 
Java :: java to kotlin 
Java :: CORS with Spring Boot 
Java :: try catch block 
Java :: extends class in java 
Java :: java print array of objects 
Java :: how to use another class in java inside of an action listenrer 
Java :: enter a word and print letters java 
Java :: gatewayFilters 
Java :: generate random color java 
Java :: worldedit api copy schematic 
Java :: how to hide search from menu android studio from activity 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =