How does Xamarin.Essentials.Browser.OpenAsync open a specific browser insted of default browser?

Yongqing Huang 241 Reputation points
2022-01-19T13:03:40.367+00:00

Hi,

There are two browsers installed op the Android device: Chrome and my customer browser. The customer browser is default.

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?

Thanks in advance!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2022-01-20T03:18:09.45+00:00

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Yongqing Huang 241 Reputation points
    2022-01-20T09:10:03.777+00:00

    Thanks, Leon.

    0 comments No comments