0

I was learning concept of broadcast receivers and I wanted to make a project with which I can demonstrate triggering specific implicit receiver class which is kind of explicit broadcast.

I made 2 apps the sender app and receiver app for demonstration :

BROADCAST SENDER's MAIN ACTIVITY :

public class MainActivity extends AppCompatActivity {

//Declaring our views
TextView senderTextView;
Button sendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initializing views
    senderTextView = findViewById(R.id.senderTextView);
    sendButton = findViewById(R.id.sendButton);
    //Setting onClick Listener on sendButton
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Calling Broadcast Method
            Broadcast();
        }
    });

}
//Broadcast Method
private void Broadcast(){
    //Creating private broadcast intent
    Intent intent = new Intent("com.example.broadcastreceiver.PRIVATE_BROADCAST");
    //Here we are going to find all the apps in our mobile that have registered for this broadcast in their manifests
    //This will help us to find packages or apps registered for com.example.PRIVATE_BROADCAST action
    PackageManager packageManager = getPackageManager();
    //queryBroadcastReceivers of package manager will query all the receivers having intent filter for "com.example.PRIVATE_BROADCAST" action
    //and store receivers info in resolveInfoList
    List<ResolveInfo> resolveInfoList = packageManager.queryBroadcastReceivers(intent,0);
    //Now we will iterate over this list to find our specific receiver and trigger it
    //for each info in resolveInfoList
    for (ResolveInfo info : resolveInfoList){
        //if info's receiver class name is com.example.broadcastreceiver.CustomBroadcastReceiver (which is our receiver class in receiver app)
        if(info.activityInfo.name.equals("com.example.broadcastreceiver.CustomBroadcastReceiver")){
            //then use this info to get package name and receiver class name to make a componentName
            ComponentName componentName = new ComponentName(info.activityInfo.packageName,info.activityInfo.name);
            //now set this componentName to our intent
            intent.setComponent(componentName);
        }
    }
    //Sending our private broadcast to our android mobile
    sendBroadcast(intent);
    //setting textView
    senderTextView.setText("Broadcast Sent!");
}

BROADCAST RECEIVER'S MANIFEST :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.broadcastreceiver">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BroadcastReceiver"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".CustomBroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcastreceiver.PRIVATE_BROADCAST"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

BROADCAST RECEIVER's CustomBroadcastReceiver Class :

public class CustomBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    
        //Making a toast when broadcast is received
        Toast.makeText(context, "Broadcast Receiver App : Custom Broadcast Receiver Triggered" , Toast.LENGTH_SHORT).show();

}

BroadcastReceiver's Mainactivity has nothing.

This should trigger the toast in CustomBroadcastReciever class of Broadcast Receiver App but due to some reason toast is not appearing please help.

Thanks in advance :)

vibhum mohan
  • 25
  • 1
  • 6
  • you can refer to this answer https://stackoverflow.com/questions/33492790/how-to-send-broadcast-from-one-app-to-another-app – Rakshit Tanti Sep 12 '22 at 12:09
  • @RakshitTanti thanks but that is different case that way i know, i want to apply this way of broadcasting where we send explicit broadcast to implicit receiver – vibhum mohan Sep 12 '22 at 12:13

1 Answers1

1

Just created a demo app that works:

In app1, send the broadcast like this (obviously change package names):

public void sendBroadcast() {
    Intent broadcastIntent = new Intent("com.example.testimplicitbroadcastreceiver.PRIVATE_BROADCAST");
    broadcastIntent.setComponent(new ComponentName("com.example.testimplicitbroadcastreceiver",
            "com.example.testimplicitbroadcastreceiver.CustomBroadcastReceiver"));
    sendBroadcast(broadcastIntent);
}

In app2's manifest, register the BroadcastReceiver:

<receiver
    android:name=".CustomBroadcastReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.testimplicitbroadcastreceiver.PRIVATE_BROADCAST"/>
    </intent-filter>
</receiver>

Voila, now app2 will receive your Broadcast in the CustomBroadcastReceiver.

Jorn Rigter
  • 745
  • 1
  • 6
  • 25
  • @Jhon Rigter Means instead of using package manager to resolve all the receivers registered for this action, store it in a list then targeting one of them you are simply passing the name of the receiver by yourself and since this broadcast has intent with action, it is an example of implicit receiver but it is also targeting a specific receiver so it is also an example of explicit too... but I have one question... will it not trigger any other receivers also that are registered for same action ? – vibhum mohan Sep 14 '22 at 18:01
  • @Jhon Rigter I tried making another broadcast Receiver registering with same action but It didn't trigger as expected... I don't know what on earth was stopping such a silly idea to come to my mind... Thanks mate you helped a lot :) – vibhum mohan Sep 14 '22 at 18:35
  • No worries Vibhum! Happy coding! – Jorn Rigter Sep 15 '22 at 06:37