From f04ff7777aa2aa1d7209e61e6d14bb150b22d814 Mon Sep 17 00:00:00 2001 From: Dinis Vieira Date: Fri, 22 Mar 2024 16:31:15 +0000 Subject: [PATCH] Added specific try catch in Android launchApp to avoid the app crashing when trying to launch app package name that are not installed on the device. (#3092) --- .../Android/Services/DeviceActionService.cs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/App/Platforms/Android/Services/DeviceActionService.cs b/src/App/Platforms/Android/Services/DeviceActionService.cs index 102f1de80d0..0a3498114cc 100644 --- a/src/App/Platforms/Android/Services/DeviceActionService.cs +++ b/src/App/Platforms/Android/Services/DeviceActionService.cs @@ -72,17 +72,28 @@ public void Toast(string text, bool longDuration = false) public bool LaunchApp(string appName) { - if ((int)Build.VERSION.SdkInt < 33) + try + { + if ((int)Build.VERSION.SdkInt < 33) + { + // API 33 required to avoid using wildcard app visibility or dangerous permissions + // https://developer.android.com/reference/android/content/pm/PackageManager#getLaunchIntentSenderForPackage(java.lang.String) + return false; + } + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; + appName = appName.Replace("androidapp://", string.Empty); + var launchIntentSender = activity?.PackageManager?.GetLaunchIntentSenderForPackage(appName); + launchIntentSender?.SendIntent(activity, Result.Ok, null, null, null); + return launchIntentSender != null; + } + catch (IntentSender.SendIntentException) + { + return false; + } + catch (Android.Util.AndroidException) { - // API 33 required to avoid using wildcard app visibility or dangerous permissions - // https://developer.android.com/reference/android/content/pm/PackageManager#getLaunchIntentSenderForPackage(java.lang.String) return false; } - var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; - appName = appName.Replace("androidapp://", string.Empty); - var launchIntentSender = activity?.PackageManager?.GetLaunchIntentSenderForPackage(appName); - launchIntentSender?.SendIntent(activity, Result.Ok, null, null, null); - return launchIntentSender != null; } public async Task ShowLoadingAsync(string text)