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

前端 未结 30 2762
庸人自扰
庸人自扰 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 23:03

    a common way to achieve this is with the next code:

    String url = "http://www.stackoverflow.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url)); 
    startActivity(i); 
    

    that could be changed to a short code version ...

    Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));      
    startActivity(intent); 
    

    or :

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")); 
    startActivity(intent);
    

    the shortest! :

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
    

    happy coding!

    0 讨论(0)
  • 2020-11-21 23:05

    Webview can be used to load Url in your applicaion. URL can be provided from user in text view or you can hardcode it.

    Also don't forget internet permissions in AndroidManifest.

    String url="http://developer.android.com/index.html"
    
    WebView wv=(WebView)findViewById(R.id.webView);
    wv.setWebViewClient(new MyBrowser());
    wv.getSettings().setLoadsImagesAutomatically(true);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    wv.loadUrl(url);
    
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:06

    //OnClick Listener

      @Override
          public void onClick(View v) {
            String webUrl = news.getNewsURL();
            if(webUrl!="")
            Utils.intentWebURL(mContext, webUrl);
          }
    

    //Your Util Method

    public static void intentWebURL(Context context, String url) {
            if (!url.startsWith("http://") && !url.startsWith("https://")) {
                url = "http://" + url;
            }
            boolean flag = isURL(url);
            if (flag) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url));
                context.startActivity(browserIntent);
            }
    
        }
    
    0 讨论(0)
  • 2020-11-21 23:08

    You can also go this way

    In xml :

    <?xml version="1.0" encoding="utf-8"?>
    <WebView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
    

    In java code :

    public class WebViewActivity extends Activity {
    
    private WebView webView;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
    
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
    
     }
    
    }
    

    In Manifest dont forget to add internet permission...

    0 讨论(0)
  • 2020-11-21 23:08

    I think this is the best

    openBrowser(context, "http://www.google.com")
    

    Put below code into global class

        public static void openBrowser(Context context, String url) {
    
            if (!url.startsWith("http://") && !url.startsWith("https://"))
                url = "http://" + url;
    
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(browserIntent);
        }
    
    0 讨论(0)
  • 2020-11-21 23:08

    Kotlin

    startActivity(Intent(Intent.ACTION_VIEW).apply {
                data = Uri.parse(your_link)
            })
    
    0 讨论(0)
提交回复
热议问题