Giter Club home page Giter Club logo

broadcast-receiver's Introduction

Broadcast-Receiver

Android BroadcastReceiver that is a very important component of Android Framework. Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

Unlike activities, android BroadcastReceiver doesn’t contain any user interface. Broadcast receiver is generally implemented to delegate the tasks to services depending on the type of intent data that’s received.

Following are some of the important system wide generated intents.

  1. android.intent.action.BATTERY_LOW : Indicates low battery condition on the device.
  2. android.intent.action.BOOT_COMPLETED : This is broadcast once, after the system has finished booting
  3. android.intent.action.CALL : To perform a call to someone specified by the data
  4. android.intent.action.DATE_CHANGED : The date has changed
  5. android.intent.action.REBOOT : Have the device reboot
  6. android.net.conn.CONNECTIVITY_CHANGE : The mobile network or wifi connection is changed(or reset)

Broadcast Receiver in Android

  1. Creating a BroadcastReceiver
  2. Registering a BroadcastReceiver

Creating a BroadcastReceiver

Let’s quickly implement a custom BroadcastReceiver as shown below.

public class MyReceiver extends BroadcastReceiver {
        public MyReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
        }
    }

BroadcastReceiver is an abstract class with the onReceiver() method being abstract.

The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs.

The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.startActivity(myIntent); or context.startService(myService); respectively.

Registering the BroadcastReceiver in android app

A BroadcastReceiver can be registered in two ways.

  1. By defining it in the AndroidManifest.xml file as shown below.
<receiver android:name=".ConnectionReceiver">
    <intent-filter>
         <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

Using intent filters we tell the system any intent that matches our subelements should get delivered to that specific broadcast receiver.

  1. By defining it programmatically Following snippet shows a sample example to register broadcast receiver programmatically.
IntentFilter filter = new IntentFilter();
intentFilter.addAction(getPackageName() + "android.net.conn.CONNECTIVITY_CHANGE");
 
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, filter);

To unregister a broadcast receiver in onStop() or onPause() of the activity the following snippet can be used.

@Override
protected void onPause() {
    unregisterReceiver(myReceiver);
    super.onPause();
}

Sending Broadcast intents from the Activity

The following snippet is used to send an intent to all the related BroadcastReceivers.

Intent intent = new Intent();
intent.setAction("com.journaldev.CUSTOM_INTENT");
sendBroadcast(intent);

Don’t forget to add the above action in the intent filter tag of the manifest or programmatically.



broadcast-receiver's People

Contributors

sambhaji213 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.