In Android how to register a custom Intent filter to a broadcast receiver?


Before getting into the example, we should know what is intent filter in android. An intent filter is an instance of the IntentFilter class. Intent filters are helpful while using implicit intents, It is not going to handle in java code, we have to set it up in AndroidManifest.xml. Android must know what kind of intent it is launching so intent filters give the information to android about intent and actions.

Before launching intent, android going to test action test, category test and data test. This example demonstrate about how to use custom intent filters to a broadcast receiver in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical"
   tools:context=".MainActivity">
   <Button
      android:id="@+id/buton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="intent filter Register button" />
   <Button
      android:id="@+id/buton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="send Data" />
</LinearLayout>

In the above code we have two buttons, one is to register intent and another one is for send data to broadcast.

package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   RadioButton radioButton;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final Button button = findViewById(R.id.buton);
      final Button send = findViewById(R.id.buton1);
      send.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent=new Intent("com.example.andy.CUSTOM_INTENT");
            sendBroadcast(intent);
         }
      });
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            registerReceiver(mMessageReceiver,new IntentFilter("com.example.andy.CUSTOM_INTENT"));
         }
      });
   }
   private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
         // Extract data included in the Intent
         String message = intent.getAction();
         Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
      }
   };
   @Override
   protected void onPause() {
      super.onPause();
      unregisterReceiver(mMessageReceiver);
   }
}

When you click on button it going to register receiver using custom broadcast action as shown below -

registerReceiver(mMessageReceiver,new IntentFilter("com.example.andy.CUSTOM_INTENT"));

It is a dynamic broadcast receiver so we should unregister onPause() as shown below-

unregisterReceiver(mMessageReceiver);

There is one more button to send brodcast. it going to send action and data to register receiver as shown below -

Intent intent=new Intent("com.example.andy.CUSTOM_INTENT");
sendBroadcast(intent);

It is dynamic broadcast register so we dont need to create a new receiver file for broadcast receiver so we have declare broadcast receiver in same activity as shown below -

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
      // Extract data included in the Intent
      String message = intent.getAction();
      Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
   }
};

In the above code it going to take data from intent as action and print on Toast. Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

First we have register a receiver by clicking "intent filter register button" and click on send data to show toast as shown below -

Click here to download the project code

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements