Broadcast Receiver Class and Registerreceiver Method

Broadcast Receiver class and registerReceiver method

Android has intent action for broadcast receiver. BroadCast receiver will be trigger when it listen any action which registered within it.

Now we will take one example :
That we need to listen the action of "whenever any bluetooth device connect to our device". For that android has it fix action android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED

So you can get it via manifest & registration also

BY Manifest Registration:

Put this in your manifest

<receiver android:name="MyBTReceiver">
<intent-filter>
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
</intent-filter>
</receiver>

Create MyBTReceiver.class

public class MyBTReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

if(intent.getAction().equals("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
Log.d(TAG,"Bluetooth connect");
}
}
}

That was the simplest broadcast Receiver.

Now,
if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). You can also register it within your existing class file. you also need to unregister it onDestroy().
here, you dont need any broadcast registration in manifest except activity registration

For example

public class MainActivity extends Activity {

IntentFilter filter1;

@Override
public void onCreate() {
filter1 = new IntentFilter("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED");
registerReceiver(myReceiver, filter1);
}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")) {
Log.d(TAG,"Bluetooth connect");
}
}
};

@Override
public void onDestroy() {
unregisterReceiver(myReceiver);
}
}

Major difference between registerReceiver() and sendBroadCast() to call the BroadCast Receivers?

First of all you have to understand what is broadcast receiver in android.

This one example

I will explain shortly.

The first example is registering as the name indicates. So after registering a particular broadcast , it will listen for any broadcast with that ACTION you provided with intentFilter. The working is same as the callback mechanism.

The second example is sending broadcast . Sending broadcast means you broadcast something, say battery change(OS Level) ,It will broadcast with an ACTION.

SO send broadcast will send some data with Action , if we listen a broadcast with Action then it will trigger on BroadcastRecicver class

Programmatically register a broadcast receiver

It sounds like you want to control whether components published in your manifest are active, not dynamically register a receiver (via Context.registerReceiver()) while running.

If so, you can use PackageManager.setComponentEnabledSetting() to control whether these components are active:

http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)

Note if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). A receiver component is primarily useful for when you need to make sure your app is launched every time the broadcast is sent.

How to register a Broadcast Receiver in a class that extends BroadcastReceiver?

You need a Context to call registerReceiver(this, filter), so you should pass it as a parameter.

public void discoveryStart(Context context)
{
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
context.registerReceiver(this, filter) //PROBLEM OCCURS HERE
}

Then you can call it from your Activity:

SearchBLDev broadcastReceiver = new SearchBLDev();
broadcastReceiver.discoveryStart(this);

Android get method of BroadcastReceiver

Actually, you could use Intents :

public class Test extends Fragment {
...
// Call this method when the condition is met.
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
getActivity().sendBroadcast(intent);
}
}

And declare that your broadcast receiver can react to this kind of Intent either through the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.BroadcastDetector"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.example.Broadcast" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>

or programmatically

IntentFilter filter = new IntentFilter("com.example.Broadcast");

MyReceiver receiver = new MyReceiver();
getActivity().registerReceiver(receiver, filter);

Then you can intercept this Intent in the receiver :

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

}
}

Android - Register/Unregister Receiver Dynamically using a Helper Class with Static Methods?

I managed to solve my issue by adding Field entries for static BroadcastReceiver myReceiver and then assigning it like myReceiver= ... inside of my static method containing the actual BroadcastReceiver code.. Furthermore, I realized I could avoid the BroadcastReceiver being killed upon calling finish(); simply by replacing context.registerReceiver with context.getApplicationContext.registerReceiver. Problem(s) solved!

Broadcast Receiver in kotlin

you can do it in the following way

Create a broadcast receiver object in your activity class

val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(contxt: Context?, intent: Intent?) {
when (intent?.action) {
BROADCAST_DEFAULT_ALBUM_CHANGED -> handleAlbumChanged()
BROADCAST_CHANGE_TYPE_CHANGED -> handleChangeTypeChanged()
}
}
}

Register broadcast receiver in onCreate() function of your activity

 LocalBroadcastManager.getInstance(this)
.registerReceiver(broadCastReceiver, IntentFilter(BROADCAST_DEFAULT_ALBUM_CHANGED))

unregister it in ondestroy function of your activity

LocalBroadcastManager.getInstance(this)
.unregisterReceiver(broadCastReceiver)


Related Topics



Leave a reply



Submit