Broadcast Receiver In Android

Broadcast Receiver

Broadcast receivers responds to the broadcast messages to the system itself. for example if you plugged in your ear phone to your mobile it will show you a message that ear phones are plugged in.
There are two important steps, they are:
  1. Creating the broadcast receiver.
  2. Registering the broadcast receiver.
Creating the broadcast receiver:
  • Create a class called Receiver, right click on java->New->Java class->main->class name as Receiver.
  • Extend a BroadcastReceiver in Receiver class.
  • Override a method called onReceive(), by hitting ctrl+o in the class Receiver.
public class Receiver extends BroadcastReceiver {
    @Override    public void onReceive(Context context, Intent intent) {
    }
}

  • Register Broadcast receiver in Mainfest.
<receiver android:name=".Receiver">
    <intent-filter>
        <action android:name="android.intent.action.AIRPLANE_MODE"/>

    </intent-filter>
</receiver>


Example:
  • Create a new project, we have do changes only in Manifest and Java class no need to design in xml.
  • Create a new java class as Receiver and do the following.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/** * Created by HSS-24 on 2/17/2016. */

public class Receiver extends BroadcastReceiver {
    @Override    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Airplane mode on",Toast.LENGTH_LONG).show();
    }
}

  • Register in Manifest
<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=".Receiver">
            <intent-filter>
                <action android:name="android.intent.action.AIRPLANE_MODE"/>

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

</manifest>

  • Now when you hit the Airplane mode in your mobile to switch on or off it should show a toast as Airplane mode on
  • Run the App.
Output:
Thank You!!!
Please like and share...

Comments

  1. Excellent blog thanks for sharing Instagram and Facebook have provided an amazing place for new brands to grow and flourish. We can find the perfect niche for your brand on the best social media platforms. Marketing through social media brings forth global audience without all these physical boundaries. Analyze and take over the competition with ease with Adhuntt Media’s digital marketing tools and strategies.
    digital marketing agency in chennai

    ReplyDelete
  2. Nice blog thanks for sharing Pixies beauty Shop is the best place to buy cosmetics in Chennai. With thousands of premium imported brands to choose from, you’ll never run out of lipstick again. And don’t forget about the best offers and value they provide.
    beauty Shop in Chennai

    ReplyDelete

Post a Comment