Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Inside my Android app , I created a simple Broadcastreceiverclass and also Added it it Manifest file.


Now my question is , how does the Android know which Broadcastreceiver class to call, if all we can add inside the Menifest file is the actions under eceiver tag.

Also , is it such that we can only create one Broadcasreceiver class in a Android app ?
or we can create multiple number of Broadcastreceiver class ?




ADDED IN MANIFEST FILE :

<receiver android:name=".MyBroadcast">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.AIRPLANE_MODE"/>
    </intent-filter>
</receiver>




BROADCASTRECEIVER CLASS :


package com.deepesh.broadcastapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            Toast.makeText(context, "Boot completed !", Toast.LENGTH_SHORT).show();
            Log.d("Keyy","Boot completed !");
        }else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())){
            Toast.makeText(context, "Airplane mode changed !", Toast.LENGTH_SHORT).show();
            Log.d("Keyy","Airplane mode changed ! !");
        }

    }
}


What I have tried:

Tried google but no specific answer to my query I guess
Posted
Updated 24-Jul-20 4:21am
Comments
David Crow 24-Jul-20 12:36pm    
You can create as many BroadcastReceiver-derived classes as you need. Call registerReceiver() for each one.

1 solution

It is all explained in the documentation: Broadcasts overview  |  Android Developers[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900