Friday, April 2, 2021

Broadcast Receiver in Android

Broadcast Receiver 

Android apps can send or receive broadcast messages from the Android system and other Android apps. These broadcasts are sent when an event of interest occurs. 

For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).

Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast.

System Broadcast : The system automatically sends broadcasts when various system events occur, such as when the system switches in and out of airplane mode. System broadcasts are sent to all apps that are subscribed to receive the event.

Referenceshttps://developer.android.com/guide/components/broadcasts

Demo App which implement System Broadcast

Step-1: Create a Main Activity (No need to add any widget in Layout File)

Like: 

import androidx.appcompat.app.AppCompatActivity; // package name

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

IntentFilter intentfilterAirplaneMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentfilterAirplaneMode = new IntentFilter
                                    (Intent.ACTION_AIRPLANE_MODE_CHANGED);
}

}
Step-2: Create a java class (Define Broadcast Receiver) by                             
                Extending BroadcastReceiver  

 Like: 

package com.example.broadcastex1y2021;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AirModeChanged extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, " Airplane Mode Changed",
                                      Toast.LENGTH_LONG).show();
}
}
Step-3: Add the action in manifest file with the receiver (inside application tag)
 Like: 
<receiver android:name=".AirModeChanged">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>

Run your app and see the effect by changing Airplane Mode 

Demo App which implement User Defined Broadcast

Step-1: Create a Main Activity with Layout as


Add onClick on Button - I have added 
SendBroadCast

Now see the Main Activity Code 

import androidx.appcompat.app.AppCompatActivity; // package name

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

IntentFilter intentFilter;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 intentFilter = new IntentFilter("MAIS");
    }
    public void SendBroadCast(View view) {
    intent = new Intent("MAIS");
intent.putExtra("BCA", "Hi How are you");
sendBroadcast(intent);
    }
}
Step-2: Create two or more  java classes which you want to respond after   
             Broadcasting (Define Broadcast Receiver) by                       
            Extending BroadcastReceiver  

 Like: 

   FirstReceiver
    package com.example.broadcastex1y2021;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;

    public class FirstReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, " Received Message in 1 " +
intent.getStringExtra("BCA"), Toast.LENGTH_LONG).show();
     }
    }

SecondReceiver
package com.example.broadcastex1y2021;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SecondReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Received message in 2 " +
intent.getStringExtra("BCA"), Toast.LENGTH_LONG).show();
}
}

Step-3: Add the action in manifest file with the receiver (inside application tag) for                 both receiver 
 Like: 
<receiver android:name=".FirstReceiver">
<intent-filter>
<action android:name="MAIS"/>
</intent-filter>
</receiver>
<receiver android:name=".SecondReceiver">
<intent-filter>
<action android:name="MAIS"/>
</intent-filter>
</receiver>

Run your app and see the effect by clicking on Button 

1 comment:

  1. Lucky Club Casino site - Lucky Club Live
    Find out about the best luckyclub slot games, including from the best developers and online casinos, Lucky Club is an online casino with a strong focus on games.

    ReplyDelete