Skip to content

Instantly share code, notes, and snippets.

@mr5z
Created December 18, 2015 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/3c05278df1803080d69f to your computer and use it in GitHub Desktop.
Save mr5z/3c05278df1803080d69f to your computer and use it in GitHub Desktop.
LocalBroadcastManager example
package com.kinpo.android_test;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
public static final String TAG = "test.android";
public static final String ACTION_RANDOM1 = "test.action.random1";
private ImageButton mBtnFab;
private Receiver mReceiver = new Receiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReceiver = new Receiver();
mBtnFab = (ImageButton) findViewById(R.id.main_fab);
mBtnFab.setOnClickListener(this);
}
@Override
protected void onResume() {
Log.d(TAG, "registering...");
LocalBroadcastManager
.getInstance(this)
.registerReceiver(mReceiver, new IntentFilter(ACTION_RANDOM1));
super.onResume();
}
@Override
protected void onPause() {
Log.d(TAG, "unregistering...");
LocalBroadcastManager
.getInstance(this)
.unregisterReceiver(mReceiver);
super.onPause();
}
@Override
public void onClick(View v) {
Log.d(TAG, "clicked");
Intent intent = new Intent();
intent.putExtra("message", "hue hue hue");
LocalBroadcastManager
.getInstance(this)
.sendBroadcast(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment