I am trying to make connection to another application and I believe is working kind of. When press my button it shows me the logo of the other application. I am sending a package name and request code in
startActivityForResult(intent, 101)
This is my current code for when button is pressed.
etSubmit.setOnClickListener {
if (etCommand.text.isEmpty()) {
Toast.makeText(this, "Komento ei voi olla tyhjä", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Komento lähetetty", Toast.LENGTH_SHORT).show()
val customAction = "com.app.app.DevicesList"
val intent = Intent(customAction)
startActivityForResult(intent, 101)
//startActivity(intent)
}
}
This is the xamarin c# version of the code.
string customAction = "com.app.app.DevicesList";
var intent = new Intent();
intent.SetAction(customAction);
StartActivityForResult(intent, 101);
I get the other application logo once but when I press the button again I do not get the logo again.
Then I am trying to display something to the user by using the following function.
private fun onActivityResult() {
val requestCode: Number = 0
val mapper = jacksonObjectMapper()
if (requestCode == 101) {
var dl = data.GetStringExtra("DevicesList");
var devices = Json.parseToJsonElement(dl).jsonObject
//var devices = mapper.readValue<List<String>>(dl)
devices. .add(0, "Phone")
devices.add(0, "(Default)")
//var devices = Json.decodeFromString<List<String>>(dl);
//devices.in .Insert(0, "Phone");
//devices.Insert(0, "(default)");
}
}
and this is the xamarin version.
if (requestCode == 101) {
var dl = data.GetStringExtra("DevicesList");
var devices = JsonConvert.DeserializeObject<List<string>>(dl);
devices.Insert(0, "Phone");
devices.Insert(0, "(default)");
}
I am not exactly sure how to handle/or do this in kotlin.
This how I have it now currently
private fun openActivityForResult() {
val customAction = "com.Moasure.Moasure.DevList"
val intent = Intent(customAction)
resultLauncher.launch(intent)
}
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data
var dl = data?.getStringExtra("DevList");
var devices = Json.parseToJsonElement(dl).jsonObject;
//var devices = dl?.let { mapper.readValue<List<String>>(it) };
devices.add(0, "Phone")
devices.add(0, "(default)")
Log.d(devices.toString(), "devices list #################")
}
}
This is my latest attempt.
private fun openActivityForResult() {
val customAction = "com.Moasure.Moasure.DevList"
val intent = Intent(customAction)
onActivityResult(101, 101, intent)
}
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data
data?.putExtra("data", data)
Log.d(data.toString(), "data: ####")
if (result.resultCode === Activity.RESULT_OK) {
var dl = data?.getStringExtra("DevList");
if (dl != null) {
Log.d(dl.length.toString(), "devices list: ####")
val mapper = jacksonObjectMapper()
var devices = Json.parseToJsonElement(dl.toString()).jsonObject;
//var devices = dl?.let { mapper.readValue<List<String>>(it) };
//var devices = JsonConvert.DeserializeObject<List<String>>(dl);
//devices.putExtra(0, "Phone");
//devices.putExtra(0, "(default)");
Log.i(devices.toString(), "### Devices")
}
}
}
}
Length is giving me d/2 in the logcat.