Borisov
Borisov

Reputation: 55

How to create interaction between two android applications

I have 2 applications with their source codes. When I press button in application one, I need to background "press" button in application 2 (start specific action from it, not MainActivity). So, for example, do command like

send "press_button2" -> APP2

What is the best way to do it?

Upvotes: 3

Views: 198

Answers (1)

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

That's quite a generic question but you'll have to use an implicit intent from your "APP1" and read the intent action in your "APP2". The steps will be:

  1. Define the implicit intent in your APP1, something like
val sendIntent: Intent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "press_button2")
    type = "text/plain"
}
startActivity(sendIntent)
  1. In your APP2, set up your manifest to receive the specified action in an activity of your choice, using an intent filter:
<activity android:name="ClickActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>
  1. In your "APP2" handle the incoming intent in your activity, something like:
@SuppressLint("MissingSuperCall")
    override fun onCreate(savedInstanceState: Bundle?) {
        when {
            intent?.action == Intent.ACTION_SEND -> {
                if ("text/plain" == intent.type) {
                    intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
                        // Update UI to reflect text being shared
                        if (it == "press_button2"){
                            myButton.performClick()
                        }
                    }
                }
            }

        }
    }

Be aware that also other apps would be able to manage your "send text action", so android will show an app chooser to the user, you'll not be able to switch between the two apps seamlessly.

Reference here

Upvotes: 3

Related Questions