UE TAC
M1 Informatique
Cours : Jean-Claude TARBY
TDs : Mehdi BRAHMI, Cédric DUMOULIN, Jean-Claude TARBY
Cours 6
Lancer d’autres applications
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 4
Lancer une autre application
• Source:
https://developer.android.com/training/basics/intents/sending?hl=fr
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 5
Comment lancer une application ?
• Créer un « Intent »
• Explicite
• On indique le nom du package de l’appli (qui doit déjà être installée avant)
• Implicite
• On indique le type d’action recherchée, et le téléphone recherche les applis qui peuvent
répondre à l’intent
• Ajoutez les paramètres éventuels
• Lancer l’activité avec
• startActivity (dans une activité)
• Warning old tutorials ! startActivityForResult et onActivityResult depréciés !
• Launch (dans un composable)
• startActivity possible si on récupère le contexte de l’activité, mais peut poser des pbs suivant
les Intents ( solution en créant un « NEW_TASK »)
« empile » l’activité dans la BackStack
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 6
Rappel : BackStack
2023-2024 JC Tarby - UE TAC - M1 E-Services 7
Exemples
• Lancer une activité DANS LA MÊME APPLICATION (intérêt limité avec Compose)
// Lancer une autre activité depuis cette activité
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
Activité 1 Activité 2 Activité 3 Activité 1
• Cf. https://gitlab.com/m1-ue-tac/compose/startactivity-basique.git
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 8
Exemples
• Lancer une activité DANS LA MÊME APPLICATION (intérêt limité avec
Compose) val contexte = LocalContext.current.applicationContext
…
// Lancer une autre activité depuis cette activité
Button(
val intent = Intent(this, SecondActivity::class.java)
onClick = {
val intent = Intent(contexte, SecondActivity::class.java)
startActivity(intent)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
// On lance une nouvelle activité depuis un Composable -->
New task
try {
contexte.startActivity(intent)
} catch (e: Exception) {
Log.d("ERROR", "erreur ${e.message}")
Cette solution pour les slides d’après }
},
){
Text("Activité 2")
M1 Info - UE TAC } - UDL - FIL - Cristal
Jean-Claude Tarby 9
Exemples
• Lancer une page web
val webIntent: Intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.android.com"))
startActivity(webIntent)
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 10
Exemples
…= Intent(Intent.ACTION_VIEW, …
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 11
Exemples
• Appeler un n° de téléphone
val intent: Intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:0320123456"))
startActivity(intent)
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 12
Exemples
• Afficher une carte
// Map point based on address
val intent: Intent = Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"))
/ / ou Uri = Uri.parse("geo:37.422219,-122.08364?z=14") // z param is zoom level
startActivity(intent)
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 13
Exemples
• Envoyer un email
val intent = Intent(Intent.ACTION_SEND).apply {
// The intent does not have a URI, so declare the "text/plain" MIME type
type = "text/plain"
putExtra(Intent.EXTRA_EMAIL, arrayOf("
[email protected]")) // recipients
putExtra(Intent.EXTRA_SUBJECT, "Email subject")
putExtra(Intent.EXTRA_TEXT, "Email message text")
putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"))
// You can also attach multiple items by passing an ArrayList of Uris
}
startActivity(intent)
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 14
Exemples
• Ajouter un événement dans l’agenda
// Event is on January 23, 2021 -- from 7:30 AM to 10:30 AM.
val intent = Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI).apply {
val beginTime: Calendar = Calendar.getInstance().apply {
set(2021, 0, 23, 7, 30)
}
val endTime = Calendar.getInstance().apply {
set(2021, 0, 23, 10, 30)
}
putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.timeInMillis)
putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.timeInMillis)
putExtra(CalendarContract.Events.TITLE, "Ninja class")
putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo")
}
startActivity(intent)
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 15
Gérer les erreurs
• Une activité peut ne pas pouvoir être lancée
• Application inexistante par exemple…
try {
contexte.startActivity(intent)
}
catch (e: ActivityNotFoundException){
Log.d("ERROR", "pas d'activité trouvé : ${e.message}")
}
catch (e: Exception) {
Log.d("ERROR", "erreur ${e.message}")
}
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 16
Exemples
• Lancer une application directement
• Cf. slides suivants…
• Envoyer les utilisateurs sur Google Play, …
• https://developer.android.com/distribute/marketing-tools/linking-to-google-
play?hl=fr
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 17
Exemples
• Lancer une application directement (si déjà installée !)
Button(
onClick = {
val intent = Intent();
intent.setPackage("com.shazam.android"); // Remplacez par le package de l'application
cible
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // On lance une nouvelle activité
depuis un Composable --> New task
try {
contexte.startActivity(intent)
} catch (e: Exception) {
Log.d("ERROR", "erreur ${e.message}")
}
},
){
Text("Lancer Shazam")
M1 Info }- UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 19
Exemples
• Lancer une application « indirectement » depuis Play Store
Button(
onClick = {
val intent = Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=fr.univ_lille_1.ieea.jc_tarby.boite_a_meuh"))
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // On lance une nouvelle activité depuis un
Composable --> New task
try {
contexte.startActivity(intent)
} catch (e: Exception) {
Log.d("ERROR", "erreur ${e.message}")
}
},
modifier = Modifier.padding(16.dp)
){
Text("Ouvrir Boite à Meuh sur le Play Store")
} Info - UE TAC
M1 Jean-Claude Tarby - UDL - FIL - Cristal 20
Exemples
• Cf. https://gitlab.com/m1-ue-tac/compose/lancer-des-applications.git
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 22
Récupérer le résultat d’un Intent
• Basé sur ActivityResultContract et registerForActivityResult /
rememberLauncherForActivityResult
• Rappel : startActivity (dans une activité)
• Warning old tutorials ! startActivityForResult et onActivityResult depréciés !
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
}
• Détails à
https://developer.android.com/training/basics/intents/result?hl=fr
• Exemple
• https://gitlab.com/m1-ue-tac/compose/start-activity-with-result.git
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 24
Récupérer le résultat d’un Intent
• Appel depuis une activité
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
// Handle the result
var resultat = ""
result.data?.getStringExtra("jc_auto")?.let {
// Do something with the result
resultat = it
Toast.makeText(this, resultat, Toast.LENGTH_LONG).show()
}
}
}
// …
launcher.launch(Intent(this,
M1 Info - UE TAC AnotherActivity::class. java
Jean-Claude Tarby - UDL))
- FIL - Cristal 25
Récupérer le résultat d’un Intent
• Appel depuis une activité
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
// Handle the result Types de contrats
var resultat = ""
result.data?.getStringExtra("jc_auto")?.let {
// Do something with the result
resultat = it
Toast.makeText(this, resultat, Toast.LENGTH_LONG).show()
}
}
}
// …
launcher.launch(Intent(this,
M1 Info - UE TAC AnotherActivity::class. java
Jean-Claude Tarby - UDL))
- FIL - Cristal 26
Récupérer le résultat d’un Intent
var result by remember { mutableStateOf<String?>(null) }
val contexte = LocalContext.current.applicationContext
• Appel depuis un bouton val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { activityResult ->
if (activityResult.resultCode == RESULT_OK) {
// Handle the result
activityResult.data?.getStringExtra("jc_bouton")?.let {
// Do something with the result
result=it
}
}
}
//…
Button(onClick = {
launcher.launch(Intent(contexte, AnotherActivity::class.java))
}) {
Text(text = "Launch Activity for Result")
}
M1 Info - UE TAC } Tarby - UDL - FIL - Cristal
Jean-Claude 27
Récupérer le résultat d’un Intent
• L’autre activité (exemple)
val resultData = Intent().apply {
putExtra("jc_auto", "Hello from Another Activity!")
putExtra("jc_bouton", "Retour de Another activity après appel par bouton")
}
setResult(Activity.RESULT_OK, resultData)
finish()
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 28
Lire un code barre ou un QRCode
• Un exemple avec Zxing
• Source : https://dev.to/tkuenneth/integrating-zxing-android-embedded-in-a-compose-app-5ela
• Code : https://github.com/PacktPublishing/Android-UI-Development-with-Jetpack-
Compose/tree/main/chapter_09/ZxingDemo
• Ma solution : https://gitlab.com/m1-ue-tac/compose/lecture-qrcode-avec-zxing.git
• Montrer appli « is_eat_safe » sur mon tel (ancien projet Platine)
• Autre solution avec CameraX et ML Kit de Google
• Cf. doc officielle
• https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner?hl=fr
• https://developers.google.com/ml-kit/vision/barcode-scanning?hl=fr
• https://developers.google.com/ml-kit/vision/barcode-scanning/android?hl=fr#try-it-out et
exemple à https://github.com/googlesamples/mlkit/tree/master/android/vision-quickstart (code
compile encore ???)
• Il y a d’autres exemple dans ce git tels que reco faciale, détection d’objet, etc.
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 31
Lire un code barre ou un QRCode
• Un exemple avec Zxing
• Source : https://dev.to/tkuenneth/integrating-zxing-android-embedded-in-a-compose-app-5ela
• Code : https://github.com/PacktPublishing/Android-UI-Development-with-Jetpack-
Compose/tree/main/chapter_09/ZxingDemo
• Ma solution : https://gitlab.com/m1-ue-tac/compose/lecture-qrcode-avec-zxing.git
• Montrer appli « is_eat_safe » sur mon tel (ancien projet Platine)
• Autre solution avec CameraX et ML Kit de Google
• Cf. doc officielle
• https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner?hl=fr
• https://developers.google.com/ml-kit/vision/barcode-scanning?hl=fr
• https://developers.google.com/ml-kit/vision/barcode-scanning/android?hl=fr#try-it-out et
exemple à https://github.com/googlesamples/mlkit/tree/master/android/vision-quickstart (code
compile encore ???)
• Il y a d’autres exemple dans ce git tels que reco faciale, détection d’objet, etc.
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 32
Exemple de QR code à scanner
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 35
TTS (Text to Speech) / STT
• https://github.com/akash251/Text-To-Speech-Using-Jetpack-
Compose
• Cf. https://gitlab.com/m1-ue-tac/compose/text-to-speech.git
• https://github.com/akash251/Speech_To_Text_In_Compose
• Cf. https://gitlab.com/m1-ue-tac/compose/speech-to-text.git
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 36
Carte
• Avec Google, devenu « compliqué »…
• Payant, besoin d’un clé, accès limité…
• Open Street Map
• Cf. https://gitlab.com/m1-ue-tac/compose/open-street-map.git
• Pour le moment, juste affichage et zoom tactile
• Reste à faire itinéraire, etc.
• Cf. doc officielle Open Street Map à https://www.openstreetmap.fr/gps-applications-de-navigation/
• D’autres solutions existent
• MapBox : https://docs.mapbox.com/android/maps/guides/
• Map tiler : https://maptiler.fr/
• OsmAnd : https://osmand.net/
• Open Layers : https://openlayers.org/
• Stamen : http://maps.stamen.com/
• Map 3 : https://www.map3.network/
• pour la France, penser aussi à https://www.geoportail.gouv.fr/
M1 Info - UE TAC Jean-Claude Tarby - UDL - FIL - Cristal 37