Reputation: 5955
In Android, I am trying to call another dummy app from my app via dummy app's uri scheme.
And from the dummy app, when the user click on a button, I want to come back to my app with a return value.
Do you think it's possible?
In Dummy App, I make the configuration as follow:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="myapp"
android:host="my-poc-application.com"
android:pathPrefix="/id" />
</intent-filter>
And from my app, I try to re-direct to my dummy app:
String scheme = "myapp"; String host = "my-poc-application.com"; String id = "8"; Uri uri = Uri.parse(scheme + "://" + host + "/" + id); Intent intent = new Intent(Intent.ACTION_VIEW, uri); // Verify it resolves PackageManager packageManager = getPackageManager(); List activities = packageManager.queryIntentActivities(intent, 0); boolean isIntentSafe = activities.size() > 0; // Start an activity if it's safe if (isIntentSafe) { startActivityForResult(intent, 1); }
But the dummy app is not called.
Upvotes: 0
Views: 1152
Reputation: 1232
Here is my attempt :
Register the intent filter in your activity of your target app:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="target-app"
android:host="target-app.com" />
</intent-filter>
</activity>
Note: You don't need the pathPrefix
In the target application's main activity:
btnGoBack.setOnClickListener{
val resultIntent = Intent()
resultIntent.putExtra("Result", "I am the result from target app")
setResult(1000, resultIntent)
finish()
}
//Read the id you sent while calling this app
val uri = intent.data
Toast.makeText(this, uri?.path.toString(), Toast.LENGTH_SHORT).show()
Note: uri?.path will contain the /id you pass, in your example, 8
Now, in the parent app, from where you will call this target app:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnOpen.setOnClickListener{
val scheme = "target-app"
val host = "target-app.com"
val id = "8"
val uri = Uri.parse("$scheme://$host/$id")
val intent = Intent(Intent.ACTION_VIEW, uri)
// Verify it resolves
val activities = packageManager.queryIntentActivities(intent, 0)
val isIntentSafe = activities.size > 0
// Start an activity if it's safe
if (isIntentSafe) {
startActivityForResult(intent, 1)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == 1000){
Toast.makeText(this, data?.getStringExtra("Result"), Toast.LENGTH_SHORT).show()
}
}
The activity has a button named btnOpen
.
In the onActivityResult
, we get the result we sent from the target app.
This code is in kotlin, and converting it to java should be trivial.
If you have any other questions, do tell.
Regards,
Priyabrata
Upvotes: 1