Skip to content

Commit

Permalink
Added specific try catch in Android launchApp to avoid the app crashi…
Browse files Browse the repository at this point in the history
…ng when trying to launch app package name that are not installed on the device. (#3092)
  • Loading branch information
dinisvieira committed Mar 22, 2024
1 parent 6477569 commit f04ff77
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/App/Platforms/Android/Services/DeviceActionService.cs
Expand Up @@ -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)
Expand Down

0 comments on commit f04ff77

Please sign in to comment.