Reputation: 55
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
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:
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "press_button2")
type = "text/plain"
}
startActivity(sendIntent)
<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>
@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