Hello there,
I'm trying to launch ArcGis Field Maps from my Android App but it's not working (I keep getting the "Intent Not Found" exception), somebody had this issue before?
Below is a code sample of what I'm running:
package lab.openfieldmaps
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.startActivity
import lab.openfieldmaps.ui.theme.OpenFieldMapsTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
OpenFieldMapsTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
OpenFieldMapsButton()
}
}
}
}
}
@Composable
fun OpenFieldMapsButton(modifier: Modifier = Modifier) {
val context = LocalContext.current
val packageName = "com.esri.fieldmaps" // Replace with the package name of the target app
val intent = Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
setPackage(packageName)
}
Column(modifier = modifier.padding(12.dp)) {
Button(
onClick = {
startActivity(
context,
intent,
null
)
},
modifier = Modifier.fillMaxWidth()
) {
Text("Abrir Field Maps")
}
}
}
I found the solution, after some research. Below is the solution code:
package lab.openfieldmaps
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.startActivity
import lab.openfieldmaps.ui.theme.OpenFieldMapsTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
OpenFieldMapsTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
OpenFieldMapsButton(packageManager, Modifier)
}
}
}
}
}
@Composable
fun OpenFieldMapsButton(packageManager: PackageManager, modifier: Modifier = Modifier) {
val context = LocalContext.current
val packageName = "com.esri.fieldmaps"
val intent = Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName(packageName, "$packageName.MainActivity")
}
var isInstalled = false
if (intent.resolveActivity(packageManager) != null) {
Toast.makeText(context, "Applicação localizada...", Toast.LENGTH_LONG).show()
isInstalled = true
}
Column(modifier = modifier.padding(12.dp)) {
Button(
onClick = {
try {
startActivity(
context,
intent,
null
)
}
catch (e: Exception) {
Toast.makeText(context, "ERROR: ${e.message}", Toast.LENGTH_LONG).show()
Log.e(MainActivity::class.java.simpleName, "Houve um erro ao tentar executar a aplicação: ${e.message}", e)
}
},
modifier = Modifier.fillMaxWidth(),
enabled = isInstalled
) {
Text("Abrir Field Maps")
}
}
}