Skip to content

Instantly share code, notes, and snippets.

@csdear
Created March 2, 2014 19:56
Show Gist options
  • Save csdear/3670dc353849d2e628b9 to your computer and use it in GitHub Desktop.
Save csdear/3670dc353849d2e628b9 to your computer and use it in GitHub Desktop.
Broadcast Receiver : Dynamic Registration
//SingleBroadcast.java
package course.examples.BroadcastReceiver.SingleBroadcastDynamicRegistration;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SingleBroadcast extends Activity {
//1. Create fields for the Intent, IntentFilter and receiver
private static final String CUSTOM_INTENT = "course.examples.BroadcastReceiver.show_toast";
private final IntentFilter intentFilter = new IntentFilter(CUSTOM_INTENT);
private final Receiver receiver = new Receiver();
//2. Create a field for the local broadcaster manager (LBM)
private LocalBroadcastManager mBroadcastMgr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//3. onCreate getInstance of LBM and register the receiver to it.
mBroadcastMgr = LocalBroadcastManager.getInstance(getApplicationContext());
mBroadcastMgr.registerReceiver(receiver, intentFilter);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//4. Send the broadcast
mBroadcastMgr.sendBroadcast(new Intent(CUSTOM_INTENT));
}
});
}
@Override
//5. Create a method to destroy/unregister the receiver
protected void onDestroy() {
mBroadcastMgr.unregisterReceiver(receiver);
super.onDestroy();
}
}
=============================================================
//Rceiver.java
package course.examples.BroadcastReceiver.SingleBroadcastDynamicRegistration;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
//1. Extend the BrodacastReceiver
public class Receiver extends BroadcastReceiver {
private final String TAG = "Receiver";
@Override
//2. Create onReceive method to receive the Intent
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "INTENT RECEIVED");
//3. Do stuff
Vibrator v = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
Toast.makeText(context, "INTENT RECEIVED by Receiver", Toast.LENGTH_LONG).show();
}
}
=============================================================
//AndroidManifest.xml -- Doesn't require anything like a Static BR!!!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.examples.BroadcastReceiver.SingleBroadcastDynamicRegistration"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>
<application
android:allowBackup="false"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name=".SingleBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
===============================
// example xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dip"
android:text="Broadcast Intent"
android:textSize="24sp" >
</Button>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment