Communicate between two Android apps- with Intent
We can communicate between two Android apps by using two easy methods, that is with the help of intent and broadcast receivers.
Let’s see how to communicate and send and receive data between two Android app using explicit or implicit intent. Let’s see the example of Explicit Intent.
Explicit Intent
Here we “explicitly” specify the package name and activity to which we want to communicate.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener {
val intent = Intent()
intent.putExtra("data", "sending test data- Hello world")
intent.component = ComponentName("in.dazzlingapps.receiverapp", "in.dazzlingapps.receiverapp.MainActivity")
startActivity(intent)
}
}
}
here ‘com.example.conceptssss’ is the package name and ‘MainActivity’ is the activity in that package name.
so, when we start activity with this intent from our app, MainActivity is opened inside the target app along with the data passed.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val receivedData = intent.getStringExtra("data")
findViewById<TextView>(R.id.text).text = receivedData
}
}
Note- Exported flag of target activity needs to be true for other app to be able to open it.
Implicit Intent
Here we specify which type of data we want to send or how we want to communicate. Then Android system selects the app which can perform the operation and creates chooser for user to select the app from.
In the following example we communicating text data to other application.
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "Data from another app")
startActivity(Intent.createChooser(intent, "Share via"))
Communicate between two Android apps- with Broadcast receiver
Broadcast receiver can be sent from sender app and received in receiver app.
In following example we send Broadcast with action “android.example.CUSTOM_INTENT”
val intent = Intent("android.example.CUSTOM_INTENT")
intent.putExtra("key", "value")
sendBroadcast(intent)
And in Receiver app we register and receive this broadcast.
class ExampleReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val data = intent.getStringExtra("key")
Toast.makeText(context, "data received $data", Toast.LENGTH_SHORT).show()
}
}
And register this broadcast receiver dynamically:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val receiver = ExampleReceiver()
registerReceiver(receiver, IntentFilter("android.example.CUSTOM_INTENT"))
}
}
The github repository of these examples can be found here:
Sender app: https://github.com/makarand-thakare/CommunicatorSenderApp
Receiver app: https://github.com/makarand-thakare/CommunicatorReceiverApp