Pages

Chapter 10: Broadcast Receiver in Android

Broadcast Receiver example in Android

In this chapter we will learn about Broadcast receiver and how to implement the broadcast receiver in Android with a simple example.
Broadcast receivers are the events and  Android components which allows you to register for system or Application events. for example, If we have registered the android.intent.action.AIRPLANE_MODE, system event which will fire up when we hit the airplane mode on/off.
  1. Create a Broadcast receiver.
  2. Register a Broadcast receiver.

Example:
  • Create a new project as BroadCastReceiverExample in Android Studio.
  • Create a new Class, right click on MainActivity.java -> New -> Class -> name it as BroadCastReceiver.
  • In broadcast receiver class extend the Broadcastreceiver() method and import a class onReceive() by hitting cntr+o button's.

  • Creating a Broadcast receiver.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}
  • Registering a Broadcast receiver in Mainfest file.
<receiver android:name=".BroadCastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.AIRPLANE_MODE">

        </action>
    </intent-filter>
</receiver>
  • Show a Toast in onReceive() in Broadcast receiver.
File: BroadCastReceiver.java:


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

public class BroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //   Displaying Toast if airplane mode is on
        Toast.makeText(context,"Airplane mode is ON",Toast.LENGTH_SHORT).show();
    }
}

  • No need to design in main layout and main java file.
File: activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hss_24.broadcastreceiverexample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    
</RelativeLayout>
File: MainActivity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

File: Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hss_24.broadcastreceiverexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".BroadCastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.AIRPLANE_MODE">

                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

  • Run the Application.
Output:
Download project here
Thank you!!!
Please like and share...

Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment