How can I open a URL in Android's web browser from my application?

前端 未结 30 2764
庸人自扰
庸人自扰 2020-11-21 22:09

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

try {
    Intent myIntent = new Intent(Int         


        
相关标签:
30条回答
  • 2020-11-21 22:51

    Simple and Best Practice

    Method 1:

    String intentUrl="www.google.com";
    Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentUrl));
        if(webIntent.resolveActivity(getPackageManager())!=null){
            startActivity(webIntent);    
        }else{
          /*show Error Toast 
                  or 
            Open play store to download browser*/
                }
    

    Method 2:

    try{
        String intentUrl="www.google.com";
        Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentUrl));
            startActivity(webIntent);
        }catch (ActivityNotFoundException e){
                    /*show Error Toast
                            or
                      Open play store to download browser*/
        }
    
    0 讨论(0)
  • 2020-11-21 22:51

    The response of MarkB is right. In my case I'm using Xamarin, and the code to use with C# and Xamarin is:

    var uri = Android.Net.Uri.Parse ("http://www.xamarin.com");
    var intent = new Intent (Intent.ActionView, uri);
    StartActivity (intent);
    

    This information is taked from: https://developer.xamarin.com/recipes/android/fundamentals/intent/open_a_webpage_in_the_browser_application/

    0 讨论(0)
  • 2020-11-21 22:52

    Based on the answer by Mark B and the comments bellow:

    protected void launchUrl(String url) {
        Uri uri = Uri.parse(url);
    
        if (uri.getScheme() == null || uri.getScheme().isEmpty()) {
            uri = Uri.parse("http://" + url);
        }
    
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
    
        if (browserIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(browserIntent);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 22:52

    This way uses a method, to allow you to input any String instead of having a fixed input. This does save some lines of code if used a repeated amount of times, as you only need three lines to call the method.

    public Intent getWebIntent(String url) {
        //Make sure it is a valid URL before parsing the URL.
        if(!url.contains("http://") && !url.contains("https://")){
            //If it isn't, just add the HTTP protocol at the start of the URL.
            url = "http://" + url;
        }
        //create the intent
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)/*And parse the valid URL. It doesn't need to be changed at this point, it we don't create an instance for it*/);
        if (intent.resolveActivity(getPackageManager()) != null) {
            //Make sure there is an app to handle this intent
            return intent;
        }
        //If there is no app, return null.
        return null;
    }
    

    Using this method makes it universally usable. IT doesn't have to be placed in a specific activity, as you can use it like this:

    Intent i = getWebIntent("google.com");
    if(i != null)
        startActivity();
    

    Or if you want to start it outside an activity, you simply call startActivity on the activity instance:

    Intent i = getWebIntent("google.com");
    if(i != null)
        activityInstance.startActivity(i);
    

    As seen in both of these code blocks there is a null-check. This is as it returns null if there is no app to handle the intent.

    This method defaults to HTTP if there is no protocol defined, as there are websites who don't have an SSL certificate(what you need for an HTTPS connection) and those will stop working if you attempt to use HTTPS and it isn't there. Any website can still force over to HTTPS, so those sides lands you at HTTPS either way


    Because this method uses outside resources to display the page, there is no need for you to declare the INternet permission. The app that displays the webpage has to do that

    0 讨论(0)
  • 2020-11-21 22:54

    The Kotlin answer:

    val browserIntent = Intent(Intent.ACTION_VIEW, uri)
    ContextCompat.startActivity(context, browserIntent, null)
    

    I have added an extension on Uri to make this even easier

    myUri.openInBrowser(context)
    
    fun Uri?.openInBrowser(context: Context) {
        this ?: return // Do nothing if uri is null
    
        val browserIntent = Intent(Intent.ACTION_VIEW, this)
        ContextCompat.startActivity(context, browserIntent, null)
    }
    

    As a bonus, here is a simple extension function to safely convert a String to Uri.

    "https://stackoverflow.com".asUri()?.openInBrowser(context)
    
    fun String?.asUri(): Uri? {
        try {
            return Uri.parse(this)
        } catch (e: Exception) {}
        return null
    }
    
    0 讨论(0)
  • 2020-11-21 22:54
    Intent getWebPage = new Intent(Intent.ACTION_VIEW, Uri.parse(MyLink));          
    startActivity(getWebPage);
    
    0 讨论(0)
提交回复
热议问题