Hello,
Welcome to our Microsoft Q&A platform!
In some case in this customer browser , Chrome need to be started. How does Xamarin.Essentials.Browser.OpenAsync/Launcher.TryOpenAsync open a specific browser (Chrome) instead of default browser?
You can set BrowserLaunchMode
to SystemPreferred
, Launch the optimized system browser and stay inside of your application. (Chrome Custom Tabs and SFSafariViewController). it will open url by Chrome Custom Tabs in your application.
If you want to invoke chrome application to open the url. Xamarin.Essentials.Browser.OpenAsync
cannot achieve it. But you can use dependence Service to achieve it.
Firstly, create a interface in the Forms.
public interface IOpenUrlService
{
void GetURLByChrome(string url);
}
Then invoke the dependenceService to open the url by chrome.
DependencyService.Get<IOpenUrlService>().GetURLByChrome("https://www.google.com.hk/");
In the end, Implement the interface on android platform. Achieve this is to use the Intent.ACTION_VIEW intent as normal, but add the package com.android.chrome to the intent. This works regardless of whether Chrome is the default browser and ensures exactly the same behavior as if the user had selected Chrome from the chooser list.
[assembly: Dependency(typeof(OpenUrlService))]
namespace App7.Droid
{
internal class OpenUrlService : IOpenUrlService
{
public void GetURLByChrome(string url)
{
Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(url));
intent.AddFlags( ActivityFlags.NewTask);
intent.SetPackage("com.android.chrome");
try
{
Android.App.Application.Context.StartActivity(intent);
}
catch (ActivityNotFoundException ex)
{
// Chrome browser presumably not installed so allow user to choose instead
intent.SetPackage(null);
Android.App.Application.Context.StartActivity(intent);
}
}
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.