blog posts

Broadcast Receivers training on Android

Broadcast receivers simply respond to messages broadcast from other applications or from the system itself. These messages are sometimes called events, intent.

 For example, applications can notify other applications that some data has been downloaded to the device and is available for use, so it is the Broadcast Receiver that intercepts this connection and initiates appropriate action.

There are two important steps to working with BroadcastReceiver for intent (‘s) spread across the system –

  • Create a Broadcast Receiver.
  • Broadcast Receiver Registration.

There is another step that you must create and distribute if you want to implement your own custom intentions.

Create a Broadcast Receiver

The Broadcast Receiver is implemented as a subclass of the BroadcastReceiver class and overrides the onReceive () method in which each message is received as an Intent object parameter.

public class MyReceiver extends BroadcastReceiver {

@Override

public void onReceive (Context context, Intent intent) {

Toast.makeText (context, “Intent Detected.”, Toast.LENGTH_LONG) .show ();

}

}

Broadcast Receiver Registration

An application listens to specific intentions played by registering a Broadcast Receiver in the AndroidManifest.xml file. Note that we want to register MyReceiver for the event created in the system ACTION_BOOT_COMPLETED, which will be played by the system after the boot process of the Android system is completed.

C: \ Users \ Mr \ Desktop \ broadcast.jpg

<application

android: icon = ”@ drawable / ic_launcher”

android: label = ”@ string / app_name”

android: theme = ”@ style / AppTheme”>

<receiver android: name = ”MyReceiver”>

<intent-filter>

<action android: name = ”android.intent.action.BOOT_COMPLETED”>

</action>

</intent-filter>

</receiver>

</application>

Now whenever your Android device is booted, it will be tracked by BroadcastReceiver MyReceiver and the logic implemented within onReceive () will be executed.

There are several events created by the system that are defined as definite static fields in the Intent class. The table below lists some important system events.

Number Fixed event and description
1 android.intent.action.BATTERY_CHANGEDDisplays information containing charge status, charge level and other information about the battery.
۲ android.intent.action.BATTERY_LOWIndicates low battery status in the device.
3 android.intent.action.BATTERY_OKAYShows the battery is good now after depletion.
4 android.intent.action.BOOT_COMPLETEDThis message will be played after the system boots.
5 android.intent.action.BUG_REPORTIndicates activity to report a bug.
6 android.intent.action.CALLMakes contact with the person identified by the data.
7 android.intent.action.CALL_BUTTONWhen the user presses the “call” button to go to a dialer or other suitable UI to make a call.
8 android.intent.action.DATE_CHANGEDShows the changed date.
9 android.intent.action.REBOOTRestart the device.

Play custom intentions

If you want to create and send a custom intent, you must create and send these intent using the sendBroadcast () method in your Activity class. If you use the sendStickyBroadcast (Intent) method, your Intent becomes sticky, meaning that the intent sent remains after the broadcast is over.

public void broadcastIntent (View view) {

Intent intent = new Intent ();

intent.setAction (“com.tutorialspoint.CUSTOM_INTENT”);

sendBroadcast (intent);

}

intent, com.tutorialspoint.CUSTOM_INTENT can be registered as intent created by the system.

<application

android: icon = ”@ drawable / ic_launcher”

android: label = ”@ string / app_name”

android: theme = ”@ style / AppTheme”>

<receiver android: name = ”MyReceiver”>

<intent-filter>

<action android: name = ”com.tutorialspoint.CUSTOM_INTENT”>

</action>

</intent-filter>

</receiver>

</application>

Example

This example explains how to create a BroadcastReceiver for custom intent tracking. Once you are familiar with custom intent, you can write your own program to track the intent created by the system. Follow the steps below to modify the Android application we created in the Hello World Example section –

the level Description
1 You can use Android studio to create an Android application and name it under the com.example.tutorialspoint7.myapplication package as described in the Hello World Example section.
۲ Modify the MainActivity.java file and add the broadcastIntent () method.
3 Create a new Java file called MyReceiver.java under the com.example.tutorialspoint7.myapplication package to define BroadcastReceiver.
4 An application can control one or more custom and system intentions without restrictions. Any intent you want to track must be recorded in the AndroidManifest.xml file using the <receiver… /> tag.
5 Modify the default content of the res / layout / activity_main.xml file to add a button for intent playback.
6 Android studio takes care of the string.xml file without having to change the string file.
7 Run the program to launch the Android emulator and see the result of the changes made in the program.

Below is the content of the MainActivity.java file. This file can contain any of the basic life cycle methods. We have added the Intent broadcast () method to play a custom intent.

package com.example.tutorialspoint7.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

public class MainActivity extends Activity {

/ ** Called when the activity is first created. * /

@Override

public void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

}

// broadcast a custom intent.

public void broadcastIntent (View view) {

Intent intent = new Intent ();

intent.setAction (“com.tutorialspoint.CUSTOM_INTENT”); sendBroadcast (intent);

}

}

Below is the content of the MyReceiver.java file.

package com.example.tutorialspoint7.myapplication;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

/ **

* Created by TutorialsPoint7 on 8/23/2016.

* /

public class MyReceiver extends BroadcastReceiver {

@Override

public void onReceive (Context context, Intent intent) {

Toast.makeText (context, “Intent Detected.”, Toast.LENGTH_LONG) .show ();

}

}

The following section shows the modified contents of the AndroidManifest.xml file. Here we added the <service… /> tag to enter the service.

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<manifest xmlns: android = ”http://schemas.android.com/apk/res/android”

package = ”com.example.tutorialspoint7.myapplication”>

<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 = ”MyReceiver”>

<intent-filter>

<action android: name = ”com.tutorialspoint.CUSTOM_INTENT”>

</action>

</intent-filter>

</receiver>

</application>

</manifest>

Below is the contents of the res / layout / activity_main.xml file, which is a custom intent to insert a release button.

<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: paddingLeft = ”@ dimen / activity_horizontal_margin”

android: paddingRight = ”@ dimen / activity_horizontal_margin”

android: paddingTop = ”@ dimen / activity_vertical_margin”

android: paddingBottom = ”@ dimen / activity_vertical_margin”

tools: context = ”. MainActivity”>

<TextView

android: id = ”@ + id / textView1 ″

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Example of Broadcast”

android: layout_alignParentTop = ”true”

android: layout_centerHorizontal = ”true”

android: textSize = ”30dp” />

<TextView

android: id = ”@ + id / textView2 ″

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”Tutorials point“

android: textColor = ”# ff87ff09 ″

android: textSize = ”30dp”

android: layout_above = ”@ + id / imageButton”

android: layout_centerHorizontal = ”true”

android: layout_marginBottom = ”40dp” />

<ImageButton

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: id = ”@ + id / imageButton”

android: src = ”@ drawable / abc”

android: layout_centerVertical = ”true”

android: layout_centerHorizontal = ”true” />

<Button

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: id = ”@ + id / button2 ″

android: text = ”Broadcast Intent”

android: onClick = ”broadcastIntent”

android: layout_below = ”@ + id / imageButton”

android: layout_centerHorizontal = ”true” />

</RelativeLayout>

Let’s run our modified Hello World program! Let’s say you created your AVD while making environmental settings. To run the program from Android studio, open one of the project activity files and from the toolbar, click on the Run Eclipse Run Icon icon. Android Studio installs the program on your AVD and launches it, and if the program settings are correct, the following Emulator window is displayed –

C: \ Users \ Mr \ Desktop \ broadcast1.png

Now to play our custom intent, we click on the Broadcast Intent button, this program publishes the custom intent “com.tutorialspoint.CUSTOM_INTENT” which is tracked by our registered BroadcastReceiver, MyReceiver, and according to our implementation logic, a phrase The following appears at the bottom of the emulator –

C: \ Users \ Mr \ Desktop \ broadcast2.png

You can run other BroadcastReceivers to track system-generated intentions such as system boot, date change, low battery level, and more.