Search code examples
androidandroid-broadcastandroid-sharing

How to implement Circular Broadcast in android


I have two applications let us say App1 and App2. Now App1 is already working which has multiple responsibilities. Now i have developed App2 which is actually a background helper app to divide the responsibilities of App1. Now because App1 is already live and i want smooth and un-interrupted process so when App2 launches it should ask App1 to shutdown its services(responsibilities) and give necessary parameters to App2 so that it can Start(up) its services. Now since i have heard that circular broadcast will do the work but i couldn't find any documentation or exact functionality which is doing this. Any help would be much appreciated.


Solution

  • Actually Circular Broadcast is not a theoretical term and it is just being used in market. So, in simple words communication between App A and App B through Broadcast where App B must have to respond against action of App A is Circular broadcast. Here is solution. App A Manifest:

    <receiver  
            android:name=".AppAReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="APP_A_RECEIVER_FILTER" />
            </intent-filter>
        </receiver>
    

    App A Receiver:

    class AppAReceiver : BroadcastReceiver() {
    
        override fun onReceive(context: Context?, intent: Intent?) {
            val data = intent?.getStringExtra("data")
            // Here you can do your work and on response given by app B
    
        }
    }
    

    App B Manifest

    <receiver  
            android:name=".AppBReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="APP_B_RECEIVER_FILTER" />
            </intent-filter>
        </receiver>
    

    App B Broadcast receiver:

    class AppBReceiver : BroadcastReceiver() {
        
            override fun onReceive(context: Context?, intent: Intent?) {
                val data = intent?.getStringExtra("data")
                // Here you can do your work and on response given by app A
        
            }
        }
    

    Finally how to send Broadcast from each app so here is the method:

    fun sendBroadcast(context: Context, data: String? = null) {
            val serviceIntent = Intent("APP_B_RECEIVER_FILTER").apply {
                this.putExtra("data", data)
                this.setPackage("<App B Package Name>")
            }
    
            context.sendBroadcast(serviceIntent)
        }
    

    Note 1: The sendBroadcast method is showing how you can send broadcast from A to app B, you need to copy same method and update Intent String and packageName to make it work.

    Note 2: These are app level broadcast and doesn't need to be registered with Activities, therefore to make it work, Apps only need to run at least once so that app process can register Broadcast.