Broadcast receiver in android

October 17, 2012 | Rishabh Agrawal | General

A broadcast receiver is a component which allows us to register for system or application events. All registered receivers for an event will be notified by Android once this event happens.

Android system periodically broadcast messages about things that are happening, such as the battery status changed, the Wi-Fi came on, or the phone’s orientation changed. You can pick up these state changes and perform actions after intercepting them and all this is done using broadcast receivers. Broadcast receivers receive and react to broadcasts generated by system or apps .

The below sample code depicts a broadcast receiver that call LockerActivity.class when  “android.intent.action.SCREEN_OFF” is intercepted.

 

BroadcastReceiver  broadcastReceiver=new BroadcastReceiver() {

//This method is called when the BroadcastReceiver is receiving an Intent broadcast

@Override

public void onReceive(Context context, Intent paramIntent) {

String str=paramIntent.getAction();//Retrieve the general action to be performed

//screen is OFF

 

if ((!”android.intent.action.SCREEN_ON”.equals(str)) || (“android.intent.action.SCREEN_OFF”.equals(str)))

{

Intent localIntent1 = new Intent(context, LockerActivity.class);//locker class called

//If set, this activity will become the start of a new task on this history stack.

context.startActivity(localIntent1);//starting activity

}

}

};

// Register a BroadcastReceiver to be run in the main activity thread. The receiver will be

//called with any broadcast Intent that matches filter, in the main application thread.

registerReceiver(broadcastReceiver,intentfilter);

 

 

BroadcastRecievers do not have a UI but they start activities based on broadcast announcements.


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal